I'll use route
as an example.
route -n
The 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?
I'll use route
as an example.
route -n
The 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?
To print a particular line you can use
sed
:to print second line.
sed
, what isI will give a short and incomplete explanation of what is
sed
. For a complete description I suggest to seesed
info pages (runinfo 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 fromed
) it can never turn back to a previous line.sed
, asawk
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. Thensed
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.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
) character at end on line (
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 ($
) with nothing (i.e. remove them)".Other useful command are
p
andd
. Thep
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 inthe script is the string
2p
consisting on the address2
and the commandp
, 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 examplewould 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
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:
The '^' mark means "line-starts-with'. So you could be more generic with:
You can use a regular expression to tell awk what lines you want, e.g.
will output the seconds column of all rows that start with
192.168
an awk answer could also look something like:
$0 simply refers to the whole row/line
sed and awk can be a very powerful combination
regular expressions are worth while to master