Information Technology Reference
In-Depth Information
program
lowerCaseStrings
of section 12.2.1 for a non-trivial example using this
technique.
We observe that one can also store
sed
commands without the two framing
single quotes in a file (say)
sedCommands
and use
sed -f sedCommands
instead of or
in any
sh
program as above.
Applications (tagging programs for grammatical analysis):
The above example
indicates how to examine a text for the overall structure of grammatical patterns
occurring in that text. Thereby, lists of verbs, nouns and other grammatical entities
can be automatically transformed into
sed
programs that perform identification (see,
e.g.
, the listing of the UNIX command
eliminateList
in Section 12.3.4). Similarly,
a dedicated list of words can be automatically formatted into a (search) program in
another programming language.
Applications (synonyms/translations through substitution):
A custom-de- signed
sed
program similar to the above can be used to replace every word in a file by a
bracketed list of synonyms. This can be used as a feedback device to encourage stu-
dents to use a diversified vocabulary. In addition, note that
sed
can handle Japanese
kanji characters. Thus, a custom-designed
sed
program similar to the above where
every English word is replaced by a bracketed family of romaji, hiragana/katakana
and kanji characters can be used to assist translation of text documents.
Application (cleaning files of control sequences in preprocessing):
The replace-
ment in a substitution can be empty. This can,
e.g.
, be used to “clean” a
tex
/
latex
[33] file of control sequences (
e.g.
,
\subsection
).
The Format of an Addressed
sed
Command
The format of an addressed
sed
command is
Address
Command
Address
can
be omitted. In that case,
Command
is then applied to every pattern space. (On first
reading, one may think of the pattern space as being the current line of input). If
an
Address
is given, then
Command
is applied to the pattern space only if the pattern
space matches
Address
.
Address
can be a pattern (regular expression) enclosed within
slashes
/
as described in Appendix A.1, a line number not enclosed within slashes
or
$
standing for the last line. In addition, a range of addresses in the format
StartActionAddress
,
EndActionAddress
Command
can be used. In that case,
Command
is applied to every pattern space after
StartActionAddress
has been found until and
including the pattern space where
EndActionAddress
has been found.
Example:
The following program replaces
TX
with
Texas
in all lines that contain
the string
USA
.
#!/bin/sh
sed
'/USA/s/TX/Texas/g'
$1
sed
commands are terminated by either an immediately following
newline
character,
a semicolon, or the end of the program.
One may wish to process the single quote
'
using
sed
or
awk
. In a program
similar to the one listed above, the single quote
'
needs to be encoded as:
'\''
.
This means for
sh
: (1) terminate the string listing the
sed
program temporarily at
the first
'
, (2) concatenate the latter with the literal (
\
) character
'
and (3) continue
the string listing the
sed
program by concatenating with the string following the
third
'
.Ifa
sed
or
awk
program is stored in a file, then the the single quote
'
is
Search WWH ::
Custom Search