Sed & Awk: Some Examples of These Wonders
Introduction
Sed & Awk, but what are they? These are two binaries for content filtering. For example, if you have a file and you want to output only specific lines, or even replace things in a file. Of course, what I’m telling you isn’t extraordinary, but combined with regex, it becomes very powerful.
Sed
Display certain lines of a large file:
|
|
Replace all occurrences of the word “stain” with the word “whiteness” in the file my_laundry, redirecting everything to the file my_clean_laundry:
|
|
Replace all line beginnings with a “.” in the file my_laundry, redirecting everything to my_well_arranged_laundry:
|
|
Replace all line endings with a “.” in the file my_laundry, redirecting everything to my_well_arranged_laundry:
|
|
Delete all lines containing the word “stain”, redirecting everything to the file my_clean_laundry:
|
|
To delete lines matching a pattern (here we delete empty lines):
|
|
Delete all lines containing the word “stain” and replace all line endings with a “.”, redirecting to my_good_smelling_laundry:
|
|
To make modifications directly without going through another file (on the fly):
|
|
For purists, we can also do a substitution in perl:
|
|
Replace the content of a file on the fly:
|
|
Or even with the replace command (which replaces in all *.ini of the current folder):
|
|
or also:
|
|
Delete the 10th line of a file:
|
|
Delete x lines from a line number:
|
|
This will delete lines 1 to 55
Awk
Display the 2nd column (PID) of the result of a ps aux:
|
|
Display the 2nd column (PID) of the result of a ps aux, preceded by a label:
|
|
Display the 2nd column (PID) of the result of a ps aux, preceded by a label and showing the user:
|
|
Display /etc/passwd well arranged:
|
|
Use mathematical functions:
|
|
(normally displays 7, if all goes well) It is possible to have the equivalent of a grep and an awk in the same awk:
|
|
This allows me to get all IPs present on my machine.
For more examples: AWK - the reference scripting language for file processing
Grep
Grep was not planned for the program, but here it is, I’ll add it anyway :-)
To display only the root line in /etc/passwd:
|
|
or
|
|
But this last line calls another instruction (cat), which is not necessary.
To display all lines except those containing root:
|
|
Combine greps:
|
|
It’s possible to see the lines before or after the grep result. To see 2 lines before:
|
|
To see 2 lines after:
|
|
To see 2 lines before and after:
|
|
FAQ
Binary database matches
If you encounter this error during a grep, your file may be too large, so grep considers it binary. But if this is not the case, you can force it to read anyway:
|
|
The -a bypasses the restriction
Resources
Last updated 01 Jan 2014, 15:45 +0200.