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