Thursday, November 9, 2017

sed, awk and bash it

Edit the files in folder

1. Chop off few words from the 1st line
2. Reads a particular word from the 1st line and then replaces that with some other word.

More particularly 
1. Removes "p tw" from the 1st line.
2. Replace the 3rd word(in line 1) with "0" in all the lines except line 1.

$cat sample
p tw 5 4
1 2
5 2
3 4
1 5

$cat required-sample
5 4
1 2
0 2
3 4
1 0


#!/bin/bash
for file in `ls *.gr`
do
    nodes=`awk 'NR==1{print $3}' $file` #takes the 3rd word which is n or |V|
    sed -i "1s/p tw //" $file             #chops the p and tw from the 1st line
    echo $file - done $nodes
    sed -i '2,$ s/$nodes/0/g' $file     #f   
done

Thanks to Vijay R



Last Post !! Moved to new site

As I am getting old 😋, it seem like I can not remember many of my earlier tech encounters. This is the place I was logging so that I refer ...