Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sed problem , please help! 3

Status
Not open for further replies.

danhodges99

IS-IT--Management
Feb 6, 2003
21
GB
Hi,

I need to insert a line into a file underneath an existing line in the file, but am unsure as to the syntax. I'm pretty sure sed can be used though, although any ideas are more than welcome. For example:

File
----
Line 1
Line 2
Line 3
Line 4
Line 6

I need to say: Insert "Line 5" after "Line 4".
Hope this is clear :)
 
I don't know about sed, but you can do it via awk.

e.g.
txt="line five"
num=4

awk -v txt="$txt" -v num="$num" '{print $0} NR==num {print txt}' file1 > file2
mv file2 file1




 
sed '
/line4/ {
aline5
}' <file>

seems to work.
 
Hi Jad - I need something like that too, and copied your example :
sed '
/sampletext/ {
afreshlinetext
}' <infile > outfile


But I've got lots of occurences of 'sampletext' in the file,
but only want to do it once after line 10 - Any more thoughts ?

Dickie Bird (:)-)))
 
sed '{
10atext to insert
}' inpfile

HTH
Cheers,
Pravin.
 
i'd use awk in that situation ... :)

awk 'output == 1{print &quot;Output Text&quot;; output=2} /line4/ && NR>10 && output<1 {output=1} {print}' <file>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top