Dear All,
I want to search and replace the text in file using awk. but facing hard luck in that.
Please help me out!!!!
> grep Abc.De.ync.rate /tmp/sdosanjh.txt
Abc.De.ync.rate 6 write
Now, I want to replace the "6" with value say "2".
I tried the follwoing using awk.... but its not working
% chgline=`cat /tmp/sdosanjh.txt | grep Abc.De.ync.rate | awk '{print ($2)}'`
% echo $chgline
6
% awk 'BEGIN{OFS=FS="Abc.De.ync.rate"}$2==$chgline{$2=5} {print}' /tmp/sdosanjh.txt
Something like this should work:
awk '/Abc.De.ync.rate/{$2=val}{print}' val=2 /tmp/sdosanjh.txt
Regards
nawk '/Abc.De.ync.rate/{if ($2==6) $2=2; print}' /tmp/sdosanjh.txt
Gr8 franklin it worked.
But its not preserving the file structure. Its trimming the tab spaces here as well in my reply... imagine there are two tab spaces after each field
if before edit the file was
Abc.De.ync.rate 6 write
Now it is like below
Abc.De.ync.rate 2 write
Any idea how it can be preserved???
sdosanjh:
Gr8 franklin it worked.
But its not preserving the file structure. Its trimming the tab spaces here as well in my reply... imagine there are two tab spaces after each field
if before edit the file was
Abc.De.ync.rate 6 write
Now it is like below
Abc.De.ync.rate 2 write
Any idea how it can be preserved???
If you ALWAYS have 2 tabs between fields....
awk '/Abc.De.ync.rate/{$2=val}{print}' val=2 OFS='\t\t' /tmp/sdosanjh.txt
sdosanjh:
Gr8 franklin it worked.
But its not preserving the file structure. Its trimming the tab spaces here as well in my reply... imagine there are two tab spaces after each field
if before edit the file was
Abc.De.ync.rate 6 write
Now it is like below
Abc.De.ync.rate 2 write
Any idea how it can be preserved???
Try this:
awk '/Abc.De.ync.rate/{$2=val}{print $1 "\t\t" val "\t\t" $3;next}{print}' val=2 /tmp/sdosanjh.txt
Regards