|
Home > Archive > Unix Programming > October 2006 > Script to search an input file, insert a line and then update the file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Script to search an input file, insert a line and then update the file
|
|
| hiddenmarkov@yahoo.com 2006-10-15, 1:27 pm |
| 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 Weikusat 2006-10-15, 1:27 pm |
| hiddenmarkov@yahoo.com writes:
> 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.
ed?
| |
| Bill Pursell 2006-10-15, 1:27 pm |
| hiddenmarkov@yahoo.com 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
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. Here's a Python snippet
that seems to work:
#!/usr/bin/env python
import sys, re
f = file(sys.argv[2], 'r')
pattern = sys.argv[1]
buf = []
for line in f.xreadlines():
buf.append(line)
if re.match(pattern, line):
buf.append("Added line\n")
f.close()
f = file(sys.argv[2], 'w')
for line in buf:
f.write(line)
| |
| Michal Nazarewicz 2006-10-15, 7:39 pm |
| "Bill Pursell" <bill.pursell@gmail.com> writes:
> hiddenmarkov@yahoo.com wrote:
>
> 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--
|
|
|
|
|