[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-ed] Not failing on no match.

From: Bob Proulx
Subject: Re: [Bug-ed] Not failing on no match.
Date: Mon, 22 Sep 2008 09:33:23 -0600
User-agent: Mutt/1.5.13 (2006-08-11)

Steve Traylen wrote:
> How do I avoid an error when a match does not work. I want to move to
> a line if it exists or else just stay where I am.
> ...
> m/string/
> ?
> q
> # echo $?
> 2
> In this case string is not in /etc/passwd and I want to just carry on
> editing after this.

Since there haven't been any other suggestions I will jump in...

The way I would do what you want is to insert a sentinel node first
that is guaranteed to match and then perform the search.  You will
either find the text you are looking for or you will hit your sentinel
line that you added that you are guaranteed to match.  Then after the
search delete your sentinel line.

cat >testfile1 <<EOF
something in file

cat >testfile2 <<EOF

cat >cmds <<EOF
# Add a known matching line.
something, is my marker
# Search for the possibly there or possibly not there line.
# Will match our added line if not originally there.
# Delete the known matching line.
# The file is modified and must be written.
w /dev/null

ed -s testfile1 < cmds ; echo $?
something in file

ed -s testfile2 < cmds ; echo $?
something, is my marker


P.S. There is an old computer science joke that asks how does a
computer scientist go hunting?  First insert a known animal so that
the algorithm is guaranteed to terminate...  :-)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]