5

I have a temp file, and I want to grep the only word that matches the pattern instead of whole word. i tried grep -o <pattern> file but its not giving me desired output

Input

xi29 vddf vss vddf vss int_s s2 rstb mg91a02_l_nd2_bulk_vt1 ln1=16n ln2=16n lp1=16n lp2=16n nf_n1=1 nf_n2=1 nf_p1=1 nf_p2=1  
xi28 vddf vss vddf vss d1 d mg91a02_l_inv_bulk_vt1 ln=16n lp=16n nf_n=1 nf_p=1 nfin_n=2 nfin_p=2 m=1  
xi25 vddf vss vddf vss int_m2 int_m1 mg91a02_l_inv_bulk_vt1 ln=16n lp=16n nf_n=1 nf_p=1 

Command

grep -o 'mg91a02' temp

Output (obtained)

mg91a02
mg91a02
mg91a02

Output (desired)

mg91a02_l_nd2_bulk_vt1
mg91a02_l_inv_bulk_vt1
mg91a02_l_inv_bulk_vt1
  • @garethTheRed i want output as above .. but all i get is mg9102 version grep (GNU grep) 2.5.1 – JigarGandhi Dec 9 '14 at 7:46
6

try

 grep -E -o 'mg91a02\w+' 

where

  • -E : extended regexp
  • -o print only matched word
  • \w : not a white space
  • + one or more time
2
grep -o 'mg91a02[^ ]*' file

This means print pattern mg91a02 until not seen^ first space([^ ]) or means print everything which is not a space.

1

With Perl regular expressions:

grep -Po '\w*mg91a02\w*'

For example:

$ grep -Po top .zshrc
top
top
top
top
top
top
top
$ grep -Po '\w*top\w*' .zshrc
setopt
setopt
autopushd
setopt
setopt
setopt
setopt
  • You don't need to add -P – Avinash Raj Dec 9 '14 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.