Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

2 Answers 2

up vote 6 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

 
discard

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.