Adding text to the end of the specific line in a file(only to the first occurrence of it)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding text to the end of the specific line in a file(only to the first occurrence of it)
# 1  
Old 09-05-2012
Adding text to the end of the specific line in a file(only to the first occurrence of it)

Hi,

I want to add a text to the end of the specific line in a file. Now my file looks like this:
Code:
999
111
222
333
111
444

I want to add the string " 555" to the end of the first line contaning 111. Moreover, I want to insert a newline after this line containg the "000" string. The output should look like this:
Code:
999
111 555
000
222
333
111
444

If it is possible, the input string (" 555 \n000") should be given from another file

Thanks in advance
# 2  
Old 09-05-2012
Try something like this..

Code:
awk '{if(s == "") {if($0 == "111") { s=$0 FS 555; print s;print "000"} else {print}} else{ print}}' file

This User Gave Thanks to pamu For This Post:
# 3  
Old 09-05-2012
Try...
Code:
$ head file[12]
==> file1 <==
999
111
222
333
111
444

==> file2 <==
 555
000

$ awk '/111/&&!c++{$0=$0 s};1' s="$(<file2)" file1
999
111 555
000
222
333
111
444

$

This User Gave Thanks to Ygor For This Post:
# 4  
Old 09-05-2012
awk

Hi,

Try this out,

Code:
awk '/^111$/ && !p{p=1;print $0 FS "555" RS "000";next;}1' file

Cheers,
Ranga Smilie
This User Gave Thanks to rangarasan For This Post:
# 5  
Old 09-05-2012
Thank you all very much. However, in my enviroment there works only the first sollution:
Code:
awk '{if(s == "") {if($0 == "111") { s=$0 FS 555; print s;print "000"} else {print}} else{ print}}' file

I have also another file in which I want to get the following result (the 111555 string instead of 111 555):
Code:
999
111555
000
222
333
111
444

Is it possible to use the given above solution to obtain this result?

Thanks in advance
# 6  
Old 09-05-2012
Quote:
Originally Posted by wenclu
Thank you all very much. However, in my enviroment there works only the first sollution:
Code:
awk '{if(s == "") {if($0 == "111") { s=$0 FS 555; print s;print "000"} else {print}} else{ print}}' file

I have also another file in which I want to get the following result (the 111555 string instead of 111 555):
Code:
999
111555
000
222
333
111
444

Is it possible to use the given above solution to obtain this result?

Thanks in advance
May i know your environment..?
OS..?
awk version..?

Cheers,
Ranga Smilie
# 7  
Old 09-05-2012
Hi,

OS: SunOS 5.9
Gnu Awk (gawk) 2.15, patchlevel 5
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on Adding one counter loop at the end of each line in a file

Hello All, I have file a.txt I want to add a counter loop at the end of each line in a file ill explain: i have a site h**p://test.test=Elite#1 i want to add a a counter to the number at the end of the file, that it will be like this urlLink//test.test=Elite#1 urlLink//test.test=Elite#2... (3 Replies)
Discussion started by: nexsus
3 Replies

2. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

3. Shell Programming and Scripting

adding line number to *end* of records in file

Given a file like this: abc def ghi I need to get to somestandardtext abc1 morestandardtext somestandardtext def2 morestandardtext somestandardtext ghi3 morestandardtext Notice that in addition to the standard text there is the line number added in as well. What I conceived is... (4 Replies)
Discussion started by: edstevens
4 Replies

4. Shell Programming and Scripting

help with sed adding line to end of file

sed '$a\ hello' books hi i am trying to use sed to append hello to the end of the file books, but for some reason i can't get it work. It keeps sayin command garbled. Anyone know what I'm doing wrong. this is in a ksh script as well. (3 Replies)
Discussion started by: bjhum33
3 Replies

5. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

6. Shell Programming and Scripting

adding new line after finding specific text

hello i need some help here are the contents of my file. test.txt this is filename 1.mp3 http://www.url.com/filenamehashed filename 2.mp3 http://www.url.com/fileamehashed something_else.zip http://www.url.com/filenamehashed so this file has 100 of these lines filename url I would... (9 Replies)
Discussion started by: mscice
9 Replies

7. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

8. Shell Programming and Scripting

Adding multiple line at the end of the file

I have 2 files which contains the following lines file1.txt line4 line5 line6 file2.txt line1 line2 line3 When i execute a script , I want my file2.txt will looks like this: line1 line2 line3 line4 line5 (2 Replies)
Discussion started by: kaibiganmi
2 Replies

9. Shell Programming and Scripting

Adding new line at the end of file

Hi I have few files. For some files the cursor is at the end of last line. For other files, cursor is at the new line at the end. I want to bring the cursor down to next line for the files that are having cursor at the end of last line In otherwords, I want to introduce a blank line at the... (5 Replies)
Discussion started by: somesh_p
5 Replies

10. Shell Programming and Scripting

adding text to end of each line in a file

I'm needing to add a "hour:min" to the end of each line in a document. The document in this case is only going to be one line. if this inserts it at the end, what needs to be changed to add something at the end... /bin/echo "%s/^/$filler/g\nwq!" | ex -s $oFile Thank you... (2 Replies)
Discussion started by: cubs0729
2 Replies
Login or Register to Ask a Question