Why grep doesn’t show me the matching word until the next space

greptext processing

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

Best Answer

try

 grep -E -o 'mg91a02\w+' 

where

  • -E : extended regexp
  • -o print only matched word
  • \w : not a white space
  • + one or more time
Related Question