March 1997
Intermediate to advanced
432 pages
11h 31m
English
Contributed by Tom Christiansen
Here’s a sed script I use to extract a termcap entry. It works for any termcap-like file, such as disktab. For example:
$ gent vt100extracts the vt100 entry from termcap, while:
$ gent eagle /etc/disktabgets the eagle entry from disktab. Now I know it could have been done in C or Perl, but I did it a long time ago. It’s also interesting because of the way it passes options into the sed script. I know, I know: it should have been written in sh not csh, too.
#!/bin/csh -f
set argc = $#argv
set noglob
set dollar = '$'
set squeeze = 0
set noback="" nospace=""
rescan:
if ( $argc > 0 && $argc < 3 ) then
if ( "$1" =~ -* ) then
if ( "-squeeze" =~ $1* ) then
set noback='s/\\//g' nospace='s/^[ ]*//'
set squeeze = 1
shift
@ argc --
goto rescan
else
echo "Bad switch: $1"
goto usage
endif
endif
set entry = "$1"
if ( $argc == 1 ) then
set file = /etc/termcap
else
set file = "$2"
endif
else
usage:
echo "usage: `basename $0` [-squeeze] entry [termcapfile]"
exit 1
endif
sed -n -e \
"/^${entry}[|:]/ {\
:x\
/\\${dollar}/ {\
${noback}\
${nospace}\
p\
n\
bx\
}\
${nospace}\
p\
n\
/^ / {\
bx\
}\
}\
/^[^ ]*|${entry}[|:]/ {\
:y\
/\\${dollar}/ {\
${noback}\
${nospace}\
p\
n\
by\
}\
${nospace}\
p\
n\
/^ / {\
by\
}\
}" < $fileOnce you get used to reading awk scripts, they seem so much easier to understand than all but the simplest sed script. It can be a painstaking task to figure out what a small sed script like the one shown here ...
Read now
Unlock full access