Re: Script to search an input file, insert a line and then update the file
- From: Michal Nazarewicz <mina86@xxxxxxx>
- Date: Sun, 15 Oct 2006 23:51:17 +0200
"Bill Pursell" <bill.pursell@xxxxxxxxx> writes:
hiddenmarkov@xxxxxxxxx wrote:
Hi,
Could any expert out there give me a hint / command used or even a
sample code in writing a script which can perform the function in the
subject line, i.e. read in a file, search for specific string, if the
string is found, insert a line and then close the file? Any suggestion
is appreciated.
NewB.
Rainer's suggestion of ed is cool:
$ echo -e -n '/pattern/a\ninserted text\n.\nw\nq\n' | ed foo
#v+
echo '/pattern/a
inserted text
..
w
q
' | ed foo
#v-
is better since -e and -n are extensions.
Should insert "inserted test" after the first occurence of "pattern" in
the file foo, but I don't even know how to easily extend it to
capture all instances of the pattern. Also, it emits an error
if the pattern doesn't appear in the file.
Sed should do then:
#v+
sed -ne 'p
/pattern/ a\
inserted text' <foo
#v-
Of course, it will insert text after all lines matching pattern.
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
.
- References:
- Script to search an input file, insert a line and then update the file
- From: hiddenmarkov
- Re: Script to search an input file, insert a line and then update the file
- From: Bill Pursell
- Script to search an input file, insert a line and then update the file
- Prev by Date: Re: strptime() get segment corruption in linux
- Next by Date: Re: Linux sockets.
- Previous by thread: Re: Script to search an input file, insert a line and then update the file
- Next by thread: strptime() get segment corruption in linux
- Index(es):
Relevant Pages
|