|
Guides All Guides and HOWTO's. |
|
Thread Tools | Display Modes |
|
|||
Taking your sed(1) skills beyond the easily swallowed 's/x/y/' porridge
1. Taking your sed(1) skills beyond the easily swallowed 's/x/y/' porridge
1.1 Introduction The DESCRIPTION from the sed(1) man page: Code:
The sed utility reads the specified files, or the standard input if no files are specified, modifying the input as specified by a list of com- mands. The input is then written to the standard output. A simple example. If we have a file with the following contents: Code:
Dear Jennifer, I miss your beautiful brown eyes. .... Code:
$ sed -e 's/Jennifer/Sally/' -e 's/brown/blue/' letter.jennifer Dear Sally, I miss your beautiful blue eyes. .... |
|
|||
1.2 Progressively cooking a nice 'al dente' 'sed' command file
The example from the previous section is not too difficult, an easily digested porridge. As an exercise to move beyond this stage and to become a 'real (wo)man', we will modify an '/etc/tttys' so we can connect to a computer with a serial connection. No more watery gui/desktop soup with hieroglyphs for us! According to the OpenBSD FAQ we have to change Code:
tty00 "/usr/libexec/getty std.9600" unknown off Code:
tty00 "/usr/libexec/getty std.9600" vt220 on secure
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump Last edited by J65nko; 2nd February 2010 at 03:01 AM. Reason: Small typo |
|
|||
1.3 Modifying the '/etc/ttys' file
Before modifying this important configuration file, we will first perform a test. Code:
$ mkdir temp ; cd temp $ cp /etc/ttys . $ ls -l total 48 -rw-r--r-- 1 j65nko j65nko 22944 Feb 2 02:32 ttys Code:
$ sed -f ../sercon ttys >ttys $ cat ttys $ ls -l ttys -rw-r--r-- 1 j65nko j65nko 0 Feb 2 02:38 ttys Please do not blame 'sed' for this behaviour, because this is regular 'sh' behaviour, as explained by the OpenBSD sh(1) man page: Code:
....the command cmd < foo > foo will open foo for reading and then truncate it when it opens it for writing, before cmd gets a chance to actually read foo. Code:
$ cp /etc/ttys ttys.orig $ sed -f ../sercon ttys.orig >ttys Code:
$ diff -u ttys.orig ttys --- ttys.orig Tue Feb 2 02:58:32 2010 +++ ttys Tue Feb 2 02:58:52 2010 @@ -16,8 +16,14 @@ ttyC9 "/usr/libexec/getty Pc" vt220 off secure ttyCa "/usr/libexec/getty Pc" vt220 off secure ttyCb "/usr/libexec/getty Pc" vt220 off secure -tty00 "/usr/libexec/getty std.9600" vt220 off secure -tty01 "/usr/libexec/getty std.9600" vt220 off secure +# --- original --- +# tty00 "/usr/libexec/getty std.9600" vt220 off secure +# --- modified --- +tty00 "/usr/libexec/getty std.9600" xterm on secure +# --- original --- +# tty01 "/usr/libexec/getty std.9600" vt220 off secure +# --- modified --- +tty01 "/usr/libexec/getty std.9600" xterm on secure tty02 "/usr/libexec/getty std.9600" unknown off tty03 "/usr/libexec/getty std.9600" unknown off tty04 "/usr/libexec/getty std.9600" unknown off $Id: Advanced-sed.xml,v 1.4 2010/02/02 02:28:13 j65nko Exp $
$Id: vbul-html.xsl,v 1.15 2010/01/16 00:58:03 j65nko Exp $
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
An example using the '$' address, mentioned in the quote by Carpetsmoker from the FreeBSD sed(1) man page:
Code:
$ cat animal.sed 1 { i\ A list of animals in our zoo\ ---------------------------- } $ { a\ ==== END of LIST ==== } Code:
$ cat animal.txt giraffe lion elephant Code:
$ sed -f animal.sed animal.txt A list of animals in our zoo ---------------------------- giraffe lion elephant ==== END of LIST ==== Code:
$ cat animal2.txt ostrich hyena gorilla Code:
$ sed -f animal.sed animal*.txt A list of animals in our zoo ---------------------------- giraffe lion elephant ostrich hyena gorilla ==== END of LIST ==== The -i flag equivalent would be Code:
$ sed -f animal.sed animal.txt ; sed -f animal.sed animal2.txt A list of animals in our zoo ---------------------------- giraffe lion elephant ==== END of LIST ==== A list of animals in our zoo ---------------------------- ostrich hyena gorilla ==== END of LIST ====
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
sed(1) command file to collect title tags from an XML document
Because of the heavy commenting, no further explanation here.
Code:
$ cat titlecollect # -- sed command file to collect all title tags from an XML document # # example lines: # <title>Automating patch generation and application for configuration files</title> # <section><title>Introduction</title> # <section><title>Automating patch creation</title # /<title>..*<\/title>/ { # replace <section> and </section> tags with 'nothing' s/<section>// s/<\/section>// # insert four blanks as indentation at begin of line s/^/ / # append a "\n" to 'hold space', then append 'pattern space' # to 'hold space' # in other words, collect all these line to 'hold space' H } # stuff to do at last line $ { # insert XML header and 'toc' root element i\ <?xml version="1.0" ?>\ <?xml-stylesheet href="XSL" type="text/xsl" ?>\ \ <toc> # get all collected stuff from hold space to pattern space g # append a closing 'toc" XML element and a comment a\ </toc>\ \ <!-- end of table of contents -> # print 'pattern space' p } # -- end of sed command file Code:
$ sed -nf titlecollect Patchcreate.xml <?xml version="1.0" ?> <?xml-stylesheet href="XSL" type="text/xsl" ?> <toc> <title>Automating patch generation and application for configuration files</title> <title>Introduction</title> <title>Patching <file>/etc/mail/aliases</file>manually</title> <title>Automating patch creation</title> <title>Using <file>patchcreate</file> to create a <file>sshd_config</file> patch</title> <title>The <file>patchcreate</file> script</title> <title>Comparison of <prog>sshd</prog> before and after patching</title> </toc> Code:
$ grep -v '^#' titlecollect /<title>..*<\/title>/ { s/<section>// s/<\/section>// s/^/ / H } $ { i\ <?xml version="1.0" ?>\ <?xml-stylesheet href="XSL" type="text/xsl" ?>\ \ <toc> g a\ </toc>\ \ <!-- end of table of contents -> p }
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
Tags |
sed, sed command file |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Xorg taking the fun out of BSD | Carpetsmoker | General software and network | 122 | 6th December 2010 01:21 PM |