david@comp.lancs.ac.uk (David T. Coffield) (12/12/86)
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 file A (all of it) with the newly inserted line. Thanks for any help. (Apologies if this seems a dumb question, but it's Friday afternoon and I'm fed up with the manual pages...)
jc@piaget.UUCP (John Cornelius) (12/18/86)
In article <107@dcl-csvax.comp.lancs.ac.uk> david@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 >file A (all of it) with the newly inserted line. > I suppose you could try /^150/a\ <THE LINE YOU WANT TO INSERT> -- John Cornelius (...!sdcsvax!piaget!jc)
aweinste@Diamond.BBN.COM (Anders Weinstein) (12/19/86)
In article <107@dcl-csvax.comp.lancs.ac.uk> david@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 >file A (all of it) with the newly inserted line. If the pattern /^150/ is only going to occur once in the input, then it's easy: /^150/a\ line-of-text-to-append-here Otherwise, you could try using two loops in the script as follows: :loop1 /^150/{ a\ line-of-text-to-append-here b loop2 } n b loop1 :loop2 n b loop2 -- Anders Weinstein <aweinste@DIAMOND.BBN.COM>
kab@reed.UUCP (Kent Black) (12/31/86)
In article <140@piaget.UUCP> jc@piaget.UUCP (John Cornelius, System Manager) writes: >In article <107@dcl-csvax.comp.lancs.ac.uk> david@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
chris@mimsy.UUCP (Chris Torek) (01/01/87)
I must have missed the original article, but the goal appears to
be to get `sed' to add a line of text after the first occurrence
of some particular pattern (here `^150'), but only the very first.
It is not difficult:
% cat foo.sed
:notyet
/^150/{a\
some\
new\
text
b copy
}
n
b notyet
:copy
n
b copy
% sed -f foo.sed
...
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP: seismo!mimsy!chris ARPA/CSNet: chris@mimsy.umd.edu