bug-ed
[Top][All Lists]
Advanced

[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
one
something in file
three
EOF

cat >testfile2 <<EOF
one
three
EOF

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

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

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

Bob

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]