Official website for Linux User & Developer
FOLLOW US ON:
Dec
14

Shell scripting for system administrators: advanced techniques

by Swayam Prakasha

Swayam Prakasha concludes his excellent three part series on shell scripting with some of the more advanced concepts, including two very useful commands…

The sed substitute command has four parts:

s      Substitute command
/../../      Delimiter
XXX      Regular Expression Pattern Search Pattern
YYY      Replacement string

One can specify a range for line numbers by inserting a comma between the numbers. So if we want to restrict our substitution to the first 50 lines, then we can use:

cvs@cn-setta-2:~> sed ‘1,50  s/abc/ABC’

And this one will perform the substitution from the 51st line to the last line in the file:

cvs@cn-setta-2:~> sed ’51,$  s/abc/ABC’

The sed command operates only on the patterns found in the incoming data. In other words, the input line is read and when a pattern is matched, the modified output is generated and the rest of the input line is scanned. One can even club several commands in a file (say for example, sample.sed) and then those commands can be applied using the -f option (this option basically reads from a file). One way of doing this is:

cvs@cn-setta-2:~> sed -f sample.sed test_input_file > test_output_file

When there are several commands in one file, each command must be on a separate line. And when you have many commands and they won’t fit neatly on one line, you can break up the line using a backslash. Sometimes we will be interested in deleting the lines that are either blank or contain only spaces. sed comes with a ‘d’ command that will help us in such cases. Here is an example to illustrate this:

cvs@cn-setta-2:~> sed -e ‘/^ *$/d’ test_input_file

Shell scripting for system administrators: advanced techniques
Fig 3 - Deleting blank lines using sed

In the above example, we have used some of the regular expression meta characters and they are:

The ^ (known as caret) matches the beginning of the line.
The $  (dollar sign) matches the end of the line.
The *  (asterisk) matches zero or more occurrences of the previous character (note that in our example, the previous character is a space).

The following sed construct helps in deleting a line containing a specific word from a file

cvs@cn-setta-2:~> sed ‘/sample/d’ input_file

Referring to our input file, test_file, take a look at the following:

cvs@cn-setta-2:~> sed ‘/sample/d’ test_file
abc def ghi klm abc opq abc

If we are interested in deleting only a specific word, we can go for the following construct:

cvs@cn-setta-2:~> sed ‘s/sample//g’ input_file

And here is an example to demonstrate such a construct:

cvs@cn-setta-2:~> sed ‘s/sample//g’ test_file
This is a program.
abc def ghi klm abc opq abc

Continue to: Page 3

Preliminary reading:
Shell scripting for system admin: the basics
Shell scripting for system admin: beyond the basics

Linux User & Developer is the magazine for the GNU Generation
Click here to try 3 issues for £1

twitter follow us

Pages: 1 2 3 4
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.

    4 Comments »

    • deabru said:

      I think it is:

      cvs@cn-setta-2:~> sed ’11 q’ < file

      without ","

    Trackbacks

    What's your opinion?

    Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

    Be nice. Keep it clean. Stay on topic. No spam.

    * Required fields