Hi,
I’m trying a thing with sed (trying to change a single line in a text file, automatically).
From the “info sed” page I got some links:
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/grabbag/tutorials/sed1line.txt
http://sed.sourceforge.net/grabbag/tutorials/do_it_with_sed.txt
on the last one, I look at this section:
There are functions, that handle user text directly (insert, append, change).
The format of that text is
command\
first line\
second line\
....\
last line
no ending \ for the last line
example in a sed script file:
/#include <termios\.h>/{
i\
#ifdef SYSV
a\
#else\
#include <sgtty.h>\
#endif
}
that would search for lines `#include <termios.h>' and then
would write
#ifdef SYSV
#include <termios.h>
#else
#include <sgtty.h>
#endif
Now, for writing the same script on one line, the -e mechanism
is needed... what follows each -e can be considered as an input
line from a sed script file, so nothing kept us from doing
sed -e '/#include <termios\.h>/{' \
-e 'i\' \
-e '#ifdef SYSV' \
-e 'a\' \
-e '#else\' \
-e '#include <sgtty.h>\' \
-e '#endif' \
-e '}'
on the command line, of course the trailing `\'s could be omitted if
we wrote all of this on one line and thus, getting a fast edit-and-test
working
So I have this sample text file to change (to feed to sed):
cer@minas-tirith:~/tmp/sedtest> cat hosts
# IP-Address Full-Qualified-Hostname Short-Hostname
#
127.0.0.1 localhost
#nm-mine-placemarker
192.168.42.167 minas-tirith.valinor Minas-Tirith
#marker
#Wifi-casa
cer@minas-tirith:~/tmp/sedtest>
And I try to run this:
cer@minas-tirith:~/tmp/sedtest> sed -n -e '/#nm-mine-placemarker/{' -e 'c\' -e 'Some text test\' -e '}' hosts
sed: -e expression #1, char 0: unmatched `{'
cer@minas-tirith:~/tmp/sedtest>
which has me baffled. The ‘{’ is matched with another ‘}’ at the end of the sequence…
(using “i” as in the sample from the documentation also fails the same).
This other one that I tried before works:
cer@minas-tirith:~/tmp/sedtest> sed -n '/#nm-mine-placemarker/{n;p;}' hosts
192.168.42.167 minas-tirith.valinor Minas-Tirith
cer@minas-tirith:~/tmp/sedtest>
Ideas? Is the documentation wrong?
–
Cheers / Saludos,
Carlos E. R.
(from 13.1 x86_64 “Bottle” (Minas Tirith))