Thursday 1 August 2019

Linux - Filter Commands

Several utilities are available to manipulate and filter files. Three of the more common utilities are discussed here: grepsed and awk.
For further explanation of grep, see the the grep command page.

Sed

The sed utility copies the named filename to the standard output, edited according to a
script of commands.
    sed -e <script> <filename>
Here is an example of a <script> command:
    sed -e s/<expression>/<replacement>/g
    <filename>
    Example: sed -e s/dog/wolf/g animals
In this example, sed will substitute all the occurances of “dog” with “wolf” in the file called animals.
If the expression contains a space, surround it with quotation marks as shown in the example below:
    Example: sed -d s/"wild dog"/wolf/g animals
Here is another example of a <script> command:
    sed -e y/<string1>/<string2>/ <filename>
    Example: sed -e y/unix/UNIX/ animals
In this example, sed will transform all the occurances of “unix” into “UNIX”. The variables <string1> and <string2> must have the same number of characters.
For more examples of <script> commands and for more information on sed, type
man sed.

Awk

The awk utility scans each input file for lines that match any of a set of patterns
specified. For each pattern specified there may be an associated action performed.
    awk ‘<pattern> {<action>}’ <filename>
The following example will print to the screen every line which contains the pattern “dog” in the file named animals.
    Example: awk '/dog/ {print}' animals
This next example will print “Found It” to the screen for every line which starts with the pattern “Tiger” in the file named animals.
    Example: awk '/^Tiger/ {print "Found It"}' animals
awk is a very powerful and complex utility. For more information on awk,
type man awk.

0 comments:

Post a Comment