Script to insert a line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script to insert a line
# 1  
Old 03-19-2014
Script to insert a line

Hi Help,
I have a file which looks like
Code:
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
Code:
123 44 55
344 55 77
600 88 99
900 87 65

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

Last edited by Don Cragun; 03-19-2014 at 03:11 PM.. Reason: Fix CODE tags, add ICODE tags.
# 2  
Old 03-19-2014
Quote:
Originally Posted by Indra2011
I need to insert a line 900 87 65 after everytime it finds the the line with $1=600
Code:
awk '$1==600{$0=$0 RS "900 87 65"}1' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-19-2014
Same thing, little bit different..
Code:
awk '1; $1==600{print s}' s="900 87 65" file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-19-2014
-------

Last edited by Indra2011; 03-19-2014 at 03:31 PM.. Reason: its wrong format ..no CODE option used...
# 5  
Old 03-19-2014
Simple sed for first request:
Code:
sed '/^600 /a\
900 87 65'

Regards,
Alister
# 6  
Old 03-19-2014
Code:
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
Code:
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
Moderator's Comments:
Mod Comment CODE and ICODE tags are easy. Hit the CODE or ICODE button in the tool bar and then type your text between the tags hitting the button created. Or enter your text, highlight that text, and hit the CODE or ICODE button to surround the selected text with the appropriate tags. You seem to be hitting the button and then positioning the cursor after the closing tag before entering the text that should be tagged.

Last edited by Don Cragun; 03-19-2014 at 04:05 PM.. Reason: Fix CODE tags, add ICODE tags.
# 7  
Old 03-19-2014
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

2. Solaris

sed insert into line 1 via script

Hi I am trying to run a sed command within a script..unfortunately it wasn't written on Solaris so doesn't work. Can anyone help with the correct coding please? It is: sed -i '1i ROWID;ORDER_ID;JOB_NAME;ORDER_TABLE' ${OUTFILE} (4 Replies)
Discussion started by: Grueben
4 Replies

3. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

4. Shell Programming and Scripting

sed - How to insert line before the first blank line following a token

Hello. I have a config file (/etc/my_config_file) which may content : # # port for HTTP (descriptions, SOAP, media transfer) traffic port=8200 # network interfaces to serve, comma delimited network_interface=eth0 # set this to the directory you want scanned. # * if have multiple... (6 Replies)
Discussion started by: jcdole
6 Replies

5. Shell Programming and Scripting

Korn Shell script to insert at specific line

Hi, I am trying to put together a Korn Shell script to insert at a specific line. The system we use is SunOS 5.10 I can get the line number by using:- num=`sed -n '/export ENV/=' ./tmp.file` Not getting much headway using the above variable's value to insert - export SYBASE=/opt/sybase15... (5 Replies)
Discussion started by: aj8200
5 Replies

6. UNIX for Dummies Questions & Answers

shell script : log to txt and insert new line everytime

Hi, I have this script, while do ps ax|grep 5060 > log.txt echo " " sleep 1 done } I want to actually put a new line everytime the loop is executed in log.txt , but I do not know how to "embed" the echo " " inside the log.txt. (so to say... (1 Reply)
Discussion started by: peuceul
1 Replies

7. Shell Programming and Scripting

Shell script to parse a line and insert a word

Hi All, I have a file like this, data1,data2,,,data5,data6. i want to write a shell script to replace data3 with "/example/string". which means my data file should look like this . data1,data2,example/string],,data5,data6. Could you guys help me to get a sed command or any other command... (8 Replies)
Discussion started by: girish.raos
8 Replies

8. Shell Programming and Scripting

awk script to compare and insert a line

Hi I want to compare a string at fixed position 10-20 for all the lines starting with 6. if they dont match it should take a copy of a line starting with 1 and insert it before the line starting with 6. How do i this? Please help Eg 1 test 1 765533 7643743 6 yes 3 5363653 373833 7... (9 Replies)
Discussion started by: appsguy616
9 Replies

9. UNIX for Dummies Questions & Answers

Need Script to insert colons in each line of file

I have a file with over 500 MAC addresses. Each address is on a new line. However, the MACs do not have ":" I need a script that will read the file, line by line and insert colons in the addresses and then print the results to a new file. current.txt looks like this 111111111111 222222222222... (4 Replies)
Discussion started by: canopus15
4 Replies

10. UNIX for Dummies Questions & Answers

How to insert new line in the data file using the script

Hi all, I have a test.dat file.In that file i have many lines of data. IN between some lines i want to insert a new line while running the test.ksh. Say for ex: In the dat file i have data like N001 100.00 N001 200.00 N001 300.00 N001 400.00 <== After this line i want to... (2 Replies)
Discussion started by: Sona
2 Replies
Login or Register to Ask a Question