Need help on grep for particular match

Hi,

Need help on grep for a particular match.

cat testfile

xx.xx.xx.xx:/share4    /nfsshare15                 nfs     defaults        yes no
xx.xx.xx.xx:/share5    /nfsshare15/sharedir1  nfs      defaults 0 0
xx.xx.xx.xx:/share6    /nfsshare15/sharedir2  nfs     defaults 0 0

Scenario 1:

cat testfile| grep -w "/nfsshare15"

expected Output:

I should get only the below line

xx.xx.xx.xx:/share4    /nfsshare15     nfs     defaults        yes no

Scanario2:

cat testfile| grep -w "/nfsshare15/sharedir1"

expected Output:

xx.xx.xx.xx:/share5    /nfsshare15/sharedir1 nfs defaults 0 0

But I am getting output like below

xx.xx.xx.xx:/share4    /nfsshare15     nfs     defaults        yes no
xx.xx.xx.xx:/share5    /nfsshare15/sharedir1 nfs defaults 0 0
xx.xx.xx.xx:/share6    /nfsshare15/sharedir2 nfs defaults 0 0

Thanks in advance

Hello sumanthupar,

Following may help you in same.

awk '($2 == "/nfsshare15/sharedir1")'  Input_file

Output will be as follows.

xx.xx.xx.xx:/share5    /nfsshare15/sharedir1  nfs      defaults 0 0

While looking for only string /nfsshare15 , you may use following then.

awk '($2 == "/nfsshare15")'  Input_file

Output will be as follows.

xx.xx.xx.xx:/share4    /nfsshare15                 nfs     defaults        yes no

Thanks,
R. Singh

You could also try something like putting the following in a file named grep_dir :

#!/bin/ksh
grep "[[:blank:]]$1[[:blank:]]" testfile

and make it executable with:

chmod +x grep_dir

Then the command:

./grep_dir /nfsshare15

produces the output:

xx.xx.xx.xx:/share4    /nfsshare15                 nfs     defaults        yes no

and the command:

./grep_dir /nfsshare15/sharedir2

produces the output:

xx.xx.xx.xx:/share6    /nfsshare15/sharedir2 nfs defaults 0 0

Although this was written and tested using the Korn shell, this will work with any shell that recognizes Bourne shell syntax.

If you want to try this on a Solaris/SunOS system, you might need to change grep to /usr/xpg4/bin/grep .

Thanks RavinderSingh13 and Don Cragun for the reply,

In my case the value "/nfsshare15" is in one variable say "$filesystem"

I tried by doing,

filesystem="/nfsshare15"
cat testfile | awk "($2 == "$filesystem")"

I got the following error,

awk: ( == /nfsshare15)
awk:   ^ syntax error
awk: cmd. line:1: ( == /sham11/suresh)
awk: cmd. line:1:                     ^ unexpected newline or end of string

And I also tried like,

cat testfile | awk '($2 == "$filesystem")'

It didn't give any output.

Thanks in advance

Hello sumanthupar,

In awk value of variables doesn't work like shell ones. Not sure about complete requirement but could you please try following and let me know if this helps.

 awk -vFILESYSTEM=$filesystem '($2 == FILESYSTEM)'  Input_file
 

Where value of variable named filesystem can be either string /nfsshare15/sharedir2 or /nfsshare15 . Also on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .

Thanks,
R. Singh

1 Like

Thanks a lot RavinderSingh. Its working perfect for me

Why don't you adapt Don Cragun's proposal?

filesystem="/nfsshare15"
grep "[[:blank:]]$filesystem[[:blank:]]" file
xx.xx.xx.xx:/share4    /nfsshare15                 nfs     defaults        yes no
filesystem="/nfsshare15/sharedir1"
grep "[[:blank:]]$filesystem[[:blank:]]" file
xx.xx.xx.xx:/share5    /nfsshare15/sharedir1  nfs      defaults 0 0