Script to insert a line

Hi Help,
I have a file which looks like

123 44 55
344 55 77
600 88 99
123 44 56
342 45 65
600 76 88

I need to insert a line 900 87 65 after everytime it finds the the line with $1=600
that means o/p should be like

123 44 55
344 55 77
600 88 99
900 87 65

and so ...
please help..
Thanks a lot in advance

awk '$1==600{$0=$0 RS "900 87 65"}1' file
1 Like

Same thing, little bit different..

awk '1; $1==600{print s}' s="900 87 65" file
1 Like

-------

Simple sed for first request:

sed '/^600 /a\
900 87 65'

Regards,
Alister

3343 2256 7687 123 44 55
3456 4786 9345 344 55 77
4231 2671 7651 600 88 99
2345 4567 4567 123 44 56
9876 6547 3452 342 45 65
2341 4564 4356 600 76 88

If the line with $4=600 then insert a line with

4231 2671 7651 900 88 99

it is exact copy of the previous line but the $4 $5 $6 changes

thanks in advance....so sorry for the last submit...This CODE always confuse me�
thanks

Note that $1==600 in the awk suggestions is a numerical comparison. This can match strings consisting of characters other than just a six followed by a zero followed by a zero. This may or may not be an issue.

Conversely, my sed approach strictly matches the three character string "600". Again, without knowing what your data means and how it's supposed to be interpreted, this may or may not be correct.

Regards,
Alister

many thnx alister.....now my problem is different as posted....if I get any help..it would be so great:-)

What have you tried? You should be able to solve this new problem by modifying either awk suggestion.

Also, pay attention to how a moderator edited your original post. Use code tags for code and data.

Regards,
Alister

1 Like

Sorry for that Alister.I will try to maintain the decorum.
As, you said that Yoda's solution will help. Yes it helps but I cannot copy the exact value of the first column, second column, third column, from the last line to current line.That's the issue.These values should be carried from the previous line and not constant across the file.
Thanks and Best Regards,
Navendra

Indra2011,
Please look at the note I just added to message #6 in this thread. Then hit the edit button for that message and compare your original message with my update. Hopefully that will clarify how to use CODE tags.

awk '{print} $4==600 {$4=900; $5=88; $6=99; print}' file

If this is not self-speaking enough, use an explicit if within { }

awk '{print} {if ($4==600) {$4=900; $5=88; $6=99; print}}' file

Made in Germany --- Your are gem...I have no words to appreciate..Many thanks for all the helps

Hi Alister. It is a numerical comparison only if $1 is numerical, like 600 , or 600.0 +600.0 , otherwise it is a string comparison.