HowTo Handle Mass Output - The sed Command

Thanks for reading thus far guys....

This is the 3rd article on a series of tutorial showing how to take a massive amount of output and extract/filter/format them into a human readable report.

Let's get straight to work;

The command "sed" allows users to find a certain string and replace that with a different string. The command will work directly on standard output (what you see on the screen) or on an existing file.

Simple Syntax (Standard Output):
$ ./some_command_to_generate_output.sh | sed 's/abc/xyz/g'

Whatever the command output is, the sed command above will replace "abc" with "xyz".

Tearing it down:
- Start with the command sed and an opening single quote: sed '
- The s immediately after the starting quote tells sed to perform a find/replace operation
- The slashes separate arguments for sed.
- The abc is expected to be "what to find" and is enclosed in the 1st two slashes
- The xya is expected to be "the new string" and is enclosed between the 2nd and 3rd slash
- The /g at the end tells sed to operate the find/replace on the entire output. Without the "g", sed will only find/replace the first occurrence.
- Finish with the final closing single quote.

Let's put this to work: Say there's a file servers.txt with the contents like below

Server Name    MFG               Product Description   Serial Number
===========    ===============   ===================   =============
wxv5238        Hewlett-Packard   ProLiant DL385G7      243897234987
erw3489        IBM               xSeries 3850          239472384798
dfw2342        Dell              PowerEdge 5900        234987234879
fxw5210        Hewlett-Packard   ProLiant DL385G2      298472938740
ter4320        HewLett-Packard   ProLiant DL385G6      345098594803

Let's say we don't like the brand name "Hewlett-Packard" to show like that. Instead we only want to show "HP". We can rewrite the file above with a single command

$ cat servers.txt | sed 's/Hewlett-Packard/HP/g'

Server Name    MFG   Product Description   Serial Number
===========    ====  ===================   =============
wxv5238        HP    ProLiant DL385G7      243897234987
erw3489        IBM   xSeries 3850          239472384798
dfw2342        Dell  PowerEdge 5900        234987234879
fxw5210        HP    ProLiant DL385G2      298472938740
ter4320        HP    ProLiant DL385G6      345098594803

How about if we want to make this change directly into the servers.txt file? Just one single command and the file gets changed and saved?

$ sed -i 's/Hewlett-Packard/HP/g' servers.txt
$ cat servers.txt

Server Name    MFG   Product Description   Serial Number
===========    ====  ===================   =============
wxv5238        HP    ProLiant DL385G7      243897234987
erw3489        IBM   xSeries 3850          239472384798
dfw2342        Dell  PowerEdge 5900        234987234879
fxw5210        HP    ProLiant DL385G2      298472938740
ter4320        HP    ProLiant DL385G6      345098594803

Articles in the Series

Part 1: The grep Command
Part 2: The awk Command 
Part 3: The sed Command
Part 4: Miscellaneous Commands

No comments:

Post a Comment

Help a friend, share your knowledge