/ Linux Reviews / Beginners: Learn Linux / Advanced Bash-Scripting Guide - en Appendix C. A Sed and Awk Micro-PrimerThis is a very brief introduction to the sed and awk text processing utilities. We will deal with only a few basic commands here, but that will suffice for understanding simple sed and awk constructs within shell scripts. sed: a non-interactive text file editor awk: a field-oriented pattern processing language with a C-like syntax For all their differences, the two utilities share a similar invocation syntax, both use regular expressions , both read input by default from stdin, and both output to stdout. These are well-behaved UNIX tools, and they work together well. The output from one can be piped into the other, and their combined capabilities give shell scripts some of the power of Perl.
C.1. SedSed is a non-interactive line editor. It receives text input, whether from stdin or from a file, performs certain operations on specified lines of the input, one line at a time, then outputs the result to stdout or to a file. Within a shell script, sed is usually one of several tool components in a pipe. Sed determines which lines of its input that it will operate on from the address range passed to it. [1] Specify this address range either by line number or by a pattern to match. For example, 3d signals sed to delete line 3 of the input, and /windows/d tells sed that you want every line of the input containing a match to "windows" deleted. Of all the operations in the sed toolkit, we will focus primarily on the three most commonly used ones. These are printing (to stdout), deletion, and substitution. Table C-1. Basic sed operators
From the command line and in a shell script, a sed operation may require quoting and certain options. sed -e '/^$/d' $filename
# The -e option causes the next string to be interpreted as an editing instruction.
# (If passing only a single instruction to "sed", the "-e" is optional.)
# The "strong" quotes ('') protect the RE characters in the instruction
#+ from reinterpretation as special characters by the body of the script.
# (This reserves RE expansion of the instruction for sed.)
#
# Operates on the text contained in file $filename.In certain cases, a sed editing command will not work with single quotes. filename=file1.txt
pattern=BEGIN
sed "/^$pattern/d" "$filename" # Works as specified.
# sed '/^$pattern/d' "$filename" has unexpected results.
# In this instance, with strong quoting (' ... '),
#+ "$pattern" will not expand to "BEGIN".
sed -n '/xzy/p' $filename # The -n option tells sed to print only those lines matching the pattern. # Otherwise all input lines would print. # The -e option not necessary here since there is only a single editing instruction. Table C-2. Examples of sed operators
Substituting a zero-length string for another is equivalent to deleting that string within a line of input. This leaves the remainder of the line intact. Applying s/GUI// to the line The most important parts of any application are its GUI and sound effectsresults in The most important parts of any application are its and sound effects A backslash forces the sed replacement command to continue on to the next line. This has the effect of using the newline at the end of the first line as the replacement string. s/^ */\ /gThis substitution replaces line-beginning spaces with a newline. The net result is to replace paragraph indents with a blank line between paragraphs. An address range followed by one or more operations may require open and closed curly brackets, with appropriate newlines. /[0-9A-Za-z]/,/^$/{
/^$/d
}
This deletes only the first of each set of consecutive blank
lines. That might be useful for single-spacing a text file,
but retaining the blank line(s) between paragraphs.
For illustrative examples of sed within shell scripts, see: For a more extensive treatment of sed, check the appropriate references in the Bibliography. Notes
/ Linux Reviews / Beginners: Learn Linux / Advanced Bash-Scripting Guide |
Meet new people Adult Dating |