Sed question

Kent Black kab at reed.UUCP
Thu Jan 1 04:37:13 AEST 1987


In article <140 at piaget.UUCP> jc at piaget.UUCP (John Cornelius, System Manager) writes:
>In article <107 at dcl-csvax.comp.lancs.ac.uk> david at comp.lancs.ac.uk (David T. Coffield) writes:
>>In "sed" how can does one form a command to do the following:
>>
>>Take file A, find the first line beginning with a 150,
			^^^^^
>>append a line of text at that point and then write out
>>
>
>/^150/a\
><THE LINE YOU WANT TO INSERT>
>
>-- 
>John Cornelius
>(...!sdcsvax!piaget!jc)

This will append the text after every line beginning with '150'.

I cannot find a brilliant, elegant solution (but then, I'm neither
brilliant nor elegant), but I found a nice crufty one:
	(don't even attempt this in csh! ;-)
	$ sed -n '/^150/ {
	> =
	> q
	> } ' filename
will write the number of the first line on which /^150/ occurs.  You
can try to work out the substitution of one sed as input for another;
I settled for:
	$ line=`sed -n '/^150/ {
	> ... filename`
	$ sed ''$line' a\
	> new text, remember\
	> to escape newlines
	> ' filename

The two single quotes before $line are necessary.

Hope someone does better; unless you have an overwhelming need for sed,
this is easier in awk.

-- kab



More information about the Comp.unix.wizards mailing list