Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

In ifcfg-eth0, I managed to grep "DEVICE=eth0" by using

grep "DEVICE=" ifcfg-eth0

But how can I grep pattern to show only "eth0"?

share|improve this question
up vote 7 down vote accepted

grep is not really the tool for this (although versions of GNU grep offer various options that do similar things). sed or awk are far better suited for this task.

awk -F= '$1 == "DEVICE" { print $2 }'
sed -n 's/^DEVICE=//p'
share|improve this answer

It is possible to do this using GNU grep with the -P option (PCRE). It is important note that not all versions of GNU grep are compiled with support for the -P option.

I would personally use awk as recommended by Chris Down, but I wanted to provide the grep answer requested for completeness.

grep -Po '(?<=DEVICE=).+' ifcfg-eth0

The (?<=) construct is known as a look-behind. It is used to find the match, but not included in the actual match.

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.