Operating System - HP-UX
1826775 Members
1487 Online
109702 Solutions
New Discussion

Re: strange behavior using grep command

 
SOLVED
Go to solution
SwissKnife
Frequent Advisor

strange behavior using grep command

Hi,

 

here is my input file

sanvg1::::lv_logiciel /logiciel
sanvg1::::loglv00  N/A
sanvg1::::lv_oradiag  /logiciel/app/oracle/diag
sanvg1::::lv_exp0x00  /exp0x00
sanvg1::::lvbase  /base

I need to grep /logiciel

but

 

cat myfile | grep -w "/logiciel" 

returns /logiciel but also /logiciel/app/oracle/diag

 

How to grep the exact substring ?

Thanks in advance,

Kind regards,

Den.

 

 

 

2 REPLIES 2
Solution

Re: strange behavior using grep command

Here's the man page for the -w option of grep:

-w                          Select only those lines containing matches
                               that form whole words. The test is that the
                               matching substring must either be at the
                               beginning of the line, or preceded by a nonword
                               constituent character.  Similarly, it
                               must be either at the end of the line or
                               followed by a non-word constituent character.
                               Word-constituent characters are letters,
                               digits, and the underscore.

 

So "/" is a "non-word consituent character", so it is considering the  "/logiciel" part to be a seperate word from "/app/oracle/diag" (which it would also consider seperate words)

Quick and dirty fix I guess would be that as you know the field you want to match on is at the end of the line you could instead use

grep "/logiciel$" myfile

 


I am an HPE Employee
Accept or Kudo
SwissKnife
Frequent Advisor

Re: strange behavior using grep command

Hi Duncan, 

Thank you so much. 

Not so dirty! 

 

In fact my code is now something like that, so I tested your trick and it works. 

VG=`echo "$theLVlist" | grep -w "${FS}$" | awk -F "::::" '{print $1}'`

 

Kind regards,

Den.