5

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"?

2 Answers 2

9

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' 
7

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.