Pipe Output of dhclient with sed

Goal

Limit output of dhclient.

Code

dhclient eth0|sed 's/answers//'

expected behaviour

RTNETLINK : Operation not permitted

got

RTNETLINK answers: Operation not permitted

Piping ifconfig works

ifconfig eth| sed 's/eth//' -> [cut the search]:flags=41...

Env

sed (GNU sed) 4.7 Packaged by Debian


The dhclient command needs to be run as root. When running it as a regular user, you get an error message

$ dhclient enp1s0:
RTNETLINK answers: Operation not permitted

Error messages are normally printed to standard error (stderr) while the pipe (|) will redirect only standard output (stdout). This is why your sed command isn't working: it doesn't actually get any input at all, what you are seeing is the standard error of the first command.

To redirect and parse standard error, you can use 2>&1 (redirect output stream 2, standard error, to output stream 1, standard output):

$ dhclient enp1s0 2>&1 | sed 's/answers//'
RTNETLINK : Operation not permitted

For more details on these two streams and the ways of redirecting each of them, see https://askubuntu.com/a/625230/85695.