Re: Script to search an input file, insert a line and then update the file



"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--
.



Relevant Pages

  • Re: regexp non-greedy matching bug?
    ... pattern in question. ... >> non-greedy or minimal fashion... ... this pattern doesn't look for one or two instances of "foo" in ... It looks for a string that starts with "foo" and maybe has a ...
    (comp.lang.python)
  • Re: regexp non-greedy matching bug?
    ... >> I want to match one or two instances of a pattern in a string. ... It looks for a string that starts with "foo" and maybe has a ... > As the founder of SPARE... ...
    (comp.lang.python)
  • Re: Predicting the Future and Kolmogorov Complexity
    ... Predictability is based on the pattern itself. ... addition to the string. ... For any prediction scheme, there are computable strings that show no ... Out of this hole comes a ~2 cm blue marble followed by a red ...
    (talk.origins)
  • Re: macro for parsing text
    ... SUB will remove from any cell selected the pattern as you described (i.e. ... Dim c As Range ... Dim Temp As String ... Dim colMatches As MatchCollection ...
    (microsoft.public.excel.programming)
  • Re: macro for parsing text
    ... SUB will remove from any cell selected the pattern as you described (i.e. ... Dim c As Range ... Dim Temp As String ... Dim colMatches As MatchCollection ...
    (microsoft.public.excel.programming)