Sed question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sed question
# 1  
Old 03-14-2003
Sed question

Hi,

I need to place a line of text into a file at a certain point, and I have been using:

sed '
/theWord/ {
a\
theNewLine
}' < sourceFile > newFile

This works a treat, trouble is that it adds theNewLine underneath EVERY occurrence of lines containing theWord. Does anyone know a way to simply add theNewLine underr the FIRST occurence of theWord, or indeed how to group multiple words together as theWord and add theNewLine under that?

Hope this is clear, thanks
Smilie
danhodges99
# 2  
Old 03-14-2003
You should be able to just replace /theWord/ with /a whole bunch of words/

If you know the words you're trying to replace are always on a certain line, such as line 2, you can use:

sed '
/several words/ {
2 a\
inserted line
}' < sourceFile > newFile

Last edited by oombera; 03-14-2003 at 10:38 AM..
# 3  
Old 03-14-2003
This is harder than it looks. The best that I can do is:
sed '
/theWord/ {
a\
theNewLine
:loop
n
b loop
}' < sourceFile > newFile
# 4  
Old 03-14-2003
Okay, this is driving me nuts...

I wrote a script that would do what you need:
Code:
lineNum=`awk "/theWord/ {print NR;exit}" sourceFile`
sed '
/theWord/{
$lineNum a\
theNewLine
}' < sourceFile > newFile

Basically, the awk command is finding the first occurence of "theWord" in the file and storing the line number in the variable "lineNum".

This would work if only $lineNum would actually resolve to the line number, which it doesn't.. sed actually wants a number, such as 1 or 2, and not a variable.. oh well, it's a start if someone can figure out how to finish it..
# 5  
Old 03-14-2003
If you're going to use a line number, there is no point to also use the pattern matching stuff. Just a line number in front of a "a" command will do it.

The problem that you are having is that you are putting a variable inside single quotes. The shell won't expand stuff inside single quotes. You need to move the line number outside of the quotes. The code you posted would work if you would just put a single quotes around the variable reference. This would give you a quoted string, then your variable outside of the quotes, and finally another quoted string.
# 6  
Old 03-14-2003
Okay I gotcha .. cool
Code:
sed `awk "/theWord/ {print NR;exit}" sourceFile`' a\
theNewLine' < sourceFile > newFile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

SED question

I am trying to write a script that will take an input text file in the format person: place: phonenumber; person: place: phonenumber; person: place: phonenumber; ... and output it using sed too: Name ######## Location ######### Phone Number... (1 Reply)
Discussion started by: jmack56
1 Replies

2. Shell Programming and Scripting

Sed question

I need to replace the numbers with a new string. How can I give a wildcard for the different # of numbers sed '/abcdef/s/abcdef=*/abcdef=999999/'<foo>foo1 From: To: abcdef=1234 abcdef=999999 abcdef=12345 abcdef=999999 abcdef=123456... (10 Replies)
Discussion started by: beppler
10 Replies

3. Shell Programming and Scripting

sed question

hi i have a file with this line: variable=/export/home/oracle I want to change the file so that the path is replaced with the value of another variable var2=/tmp/anything. how to do this in sed? thx (4 Replies)
Discussion started by: melanie_pfefer
4 Replies

4. Shell Programming and Scripting

Sed Question 1. (Don't quite know how to use sed! Thanks)

Write a sed script to extract the year, rank, and stock for the most recent 10 years available in the file top10_mktval.csv, and output in the following format: ------------------------------ YEAR |RANK| STOCK ------------------------------ 2007 | 1 | Exxon... (1 Reply)
Discussion started by: beibeiatNY
1 Replies

5. UNIX for Dummies Questions & Answers

sed question

How would I use sed to print everything on the line after the regular expresion? I have a configuration file setting several variables. cfg.dat DDB = cpptest SUDBNAME = sucpptestdb host = cpptest Example I want to search for the regular expresion 'SUDBNAME =' and print everything on... (3 Replies)
Discussion started by: orahi001
3 Replies

6. Shell Programming and Scripting

sed question

I have a file that conatins following info Policy1=U|guestRoom=test1idCode=5(1):!:Amenity2=U|RoomId=testrma=4(1):!:| GuestRoomAmenity1=U|guestRoomId=testguest1id^rmaCode=5(1):!:| I need it to look like this Policy1=U|guestRoom=test1idCode Amenity2=U|RoomId=testrmaCode... (2 Replies)
Discussion started by: arushunter
2 Replies

7. Shell Programming and Scripting

sed question

Hi, :) can any body explain the following statement sed 's/\(\)- ]//g' cheers RRK (3 Replies)
Discussion started by: ravi raj kumar
3 Replies

8. Shell Programming and Scripting

sed question

Hi, When deleting lines using sed, as i understand the lines are redirected to the standard output. What i'm unclear about is how to actually modify the file? If I write the command sed '1,2d' test it will display lines one and 2 onto the screen however the file is not modified? I think my... (5 Replies)
Discussion started by: c19h28O2
5 Replies

9. Shell Programming and Scripting

sed question (again)

hello there, I have a sed question. I have a file (temp.srv), in it it has v1_host1 v2_host2 And I have another file (temp2.srv), in it is has v1_host3_date v1_host1 v2_host2 v2_host4_date v3_host5_date I had used a script to remove the name from temp2.srv base on the name inside... (3 Replies)
Discussion started by: ahtat99
3 Replies

10. Shell Programming and Scripting

Sed Question

Hi, Is there any way to traverse the file once and look for the following conditions in one sweep instead of going over the file 3 times with different search criteria...... sed -n '/^ORA-07445/ p' /tmp/t$$ > ${OERRFILE} sed -n '/^ORA-00600/ p' /tmp/t$$ >> ${OERRFILE} ... (1 Reply)
Discussion started by: YS2002
1 Replies
Login or Register to Ask a Question