Using grep, awk, etc. to filter command output
Andrew Mclaughlin
I'll use route as an example.
route -nThe above outputs routing information. The top row is your active connection. I can manipulate the output to only the second column by doing:
route -n | awk '{print $2}'Now, how can I filter by row?
4 Answers
To print a particular line you can use sed:
route -n | sed -n 2pto print second line.
sed, what is
I will give a short and incomplete explanation of what is sed. For a complete description I suggest to see sed info pages (run info sed).
sed mean stream editor, i.e. an editor that can act on a stream (or pipe) of text data, though it can act also on files; substantially this mean that (differently from ed) it can never turn back to a previous line.
sed, as awk reads one line at a time and apply a script to that line to possibly modify it. At the end of the script, by default the modified line is output to stdout. Then sed go on the the next line.
A script is a sequence of pair address-command, if the line match the address then the command is executed.
The typical use of sed is to perform a substitution of a pattern with something, e.g.
command | sed 's/ *$//'here the string between single quotes is the script, consisting of a single address-command pair. The address is missing (it would appear before the s), in such a case the command is applied by default to all input lines. s is the command and the rest of the string are command specific instructions, saying "substitute 0 or more occurrence (*) of a space () character at end on line ($) with nothing (i.e. remove them)".
Other useful command are p and d. The p command print the current line, this is useful in combination with the -n option, that modify the default behavior to print the current line at end of script. So in
sed -n '2p'the script is the string 2p consisting on the address 2 and the command p, so the line 2 will be printed, all other line instead will not be printed, due to the -n option.
The d command delete a line, for example
sed '3,6d'would delete all lines from the third to the sixth. 3,6 is an address range. I observe that in this case one should not use the -n option, because we want to print all other lines.
Last thing, an address could be a pattern, like in
sed -n '/^#/p'this command would print all lines beginning (^) with a # character.
You can filter IN a row by piping to grep and finding a row that starts with your IP address of interest, for example, if you want to see the row for 192.168.1.1, you would do:
$ route | grep ^192.168.1.0
192.168.1.0 * 255.255.255.0 U 2 0 0 wlan0The '^' mark means "line-starts-with'. So you could be more generic with:
$ route | grep ^192.168.1
192.168.1.0 * 255.255.255.0 U 2 0 0 wlan0
192.168.111.0 * 255.255.255.0 U 0 0 0 virbr0 You can use a regular expression to tell awk what lines you want, e.g.
route -n | awk '/^192.168/ {print $2}'will output the seconds column of all rows that start with 192.168
an awk answer could also look something like:
$0 ~ ( /matchthis/ ) { print $2 }$0 simply refers to the whole row/line
sed and awk can be a very powerful combination
regular expressions are worth while to master