Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sed question

12 views
Skip to first unread message

David T. Coffield

unread,
Dec 12, 1986, 9:26:33 AM12/12/86
to
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...)

John Cornelius

unread,
Dec 17, 1986, 10:27:36 PM12/17/86
to
In article <1...@dcl-csvax.comp.lancs.ac.uk> da...@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)

Anders Weinstein

unread,
Dec 18, 1986, 4:06:09 PM12/18/86
to
In article <1...@dcl-csvax.comp.lancs.ac.uk> da...@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 <awei...@DIAMOND.BBN.COM>

Kent Black

unread,
Dec 31, 1986, 1:37:13 PM12/31/86
to
In article <1...@piaget.UUCP> j...@piaget.UUCP (John Cornelius, System Manager) writes:
>In article <1...@dcl-csvax.comp.lancs.ac.uk> da...@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 Torek

unread,
Jan 1, 1987, 8:32:42 AM1/1/87
to
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: ch...@mimsy.umd.edu

0 new messages