Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

To print a particular line you can use sed:

route -n | sed -n 2p

to 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.

share|improve this answer
That's very easy! Could you please explain what the p is for? – Brian Graham Nov 29 '11 at 23:18
@DarkSlipstream: I added a short description of sed in general, and of the p command in particular. – enzotib Nov 30 '11 at 8:53

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, you would do:

$ route | grep ^     *        U     2      0        0 wlan0

The '^' mark means "line-starts-with'. So you could be more generic with:

$ route | grep ^192.168.1     *        U     2      0        0 wlan0   *        U     0      0        0 virbr0
share|improve this answer

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

share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.