Need help on grep for particular match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on grep for particular match
# 1  
Old 11-27-2015
Need help on grep for particular match

Hi,

Need help on grep for a particular match.

Code:
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:

Code:
cat testfile| grep -w "/nfsshare15"

expected Output:

I should get only the below line

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

Scanario2:

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

expected Output:

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

But I am getting output like below

Code:
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
# 2  
Old 11-27-2015
Hello sumanthupar,

Following may help you in same.
Code:
awk '($2 == "/nfsshare15/sharedir1")'  Input_file

Output will be as follows.
Code:
xx.xx.xx.xx:/share5    /nfsshare15/sharedir1  nfs      defaults 0 0

While looking for only string /nfsshare15, you may use following then.
Code:
awk '($2 == "/nfsshare15")'  Input_file

Output will be as follows.
Code:
xx.xx.xx.xx:/share4    /nfsshare15                 nfs     defaults        yes no

Thanks,
R. Singh
# 3  
Old 11-27-2015
You could also try something like putting the following in a file named grep_dir:
Code:
#!/bin/ksh
grep "[[:blank:]]$1[[:blank:]]" testfile

and make it executable with:
Code:
chmod +x grep_dir

Then the command:
Code:
./grep_dir /nfsshare15

produces the output:
Code:
xx.xx.xx.xx:/share4    /nfsshare15                 nfs     defaults        yes no

and the command:
Code:
./grep_dir /nfsshare15/sharedir2

produces the output:
Code:
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.
# 4  
Old 11-27-2015
Thanks RavinderSingh13 and Don Cragun for the reply,

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

I tried by doing,

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

I got the following error,

Code:
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,

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

It didn't give any output.

Thanks in advance
# 5  
Old 11-27-2015
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.
Code:
 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
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 11-27-2015
Thanks a lot RavinderSingh. Its working perfect for me
# 7  
Old 11-27-2015
Why don't you adapt Don Cragun's proposal?
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep match a mask

Hi all, Im trying to use grep to match a exact mask where some characters are known and some are not. for example: j***n@email.com in this case the length is known and the position of the unknown characters are know (and will be ) any idea how I can do this? the file list is not just... (3 Replies)
Discussion started by: flamer
3 Replies

2. UNIX for Dummies Questions & Answers

Grep Files with and without match

Hi There, How do i finding files with match and without match Normally, I will use grep -l 'Hello' grep -L 'Hello World' How do we combined (2 Replies)
Discussion started by: alvinoo
2 Replies

3. Shell Programming and Scripting

Grep exact match

Hello! I have 2 files named tacs.tmp and tacDB.txt tacs.tmp looks like this 0 10235647 102700 106800 107200 1105700 tacDB.txt looks like this 100100,Mitsubishi,G410,Handheld,,0,0,0 100200,Siemens,A53,Handheld,,0,0,0 100300,Sony Ericsson,TBD (AAB-1880030-BV),Handheld,,0,0,0... (2 Replies)
Discussion started by: Cludgie
2 Replies

4. Shell Programming and Scripting

grep match two lines

Hello everyone!I have a problem with grep function in unix.I have two files.One like this one:File1(Code)>100_10_50 >100_10_56>10_45_345and the other one like this:File2(Code more... (1 Reply)
Discussion started by: giuliano
1 Replies

5. Shell Programming and Scripting

Reverse match in grep

root@localhost# echo 'server $serviceName -connectorType $connectorType -ipAddress $ipAddress -port $port -domain $domain' | cut -d "-" -f 1 O/P= server $serviceName when i grep the o/p -v nothing is displayed now how to output the all data by excluding the O/P (server $serviceName) from the... (5 Replies)
Discussion started by: kalyankalyan
5 Replies

6. Solaris

grep exact match

Hi This time I'm trying to grep for an exact match e.g cat.dog.horse.cow.bird.pig horse.dog.pig pig.cat.horse.dog horse dog dog pig.dog pig.dog.bird how do I grep for dog only so that a wc -l would result 2 in above case. Thanks in advance ---------- Post updated at 06:33 AM... (4 Replies)
Discussion started by: rob171171
4 Replies

7. Shell Programming and Scripting

Need help to grep for a title match and then make some queries after the match

Here is the sample of my file address.txt Address 1 1234 Drive way New Orleans, LA Zipcode :- 12345 Address 2 4567 Spring way Chicago, IL Zipcode :- 67890 I would like to grep for an Address title (Ex :- Address 2) , then get its zipcode and echo both in a single line. Ex :- ... (3 Replies)
Discussion started by: leo.maveriick
3 Replies

8. Shell Programming and Scripting

Pattern match in grep

Hi, I try to grep lines with pattern like float_or_int float_or_int float_or_int where float_or_int is any float or any integer of any length. I tried some : grep "* * *" FILENAME #Work for integers but not for floats grep '\<.*\> \<.*\> \<.*\>' FILENAME #Work for float but not... (4 Replies)
Discussion started by: tipi
4 Replies

9. UNIX for Advanced & Expert Users

Exact Match thru grep ?????

hey..... i do have text where the contents are like as follows, FILE_TYPE_NUM_01=FILE_TYPE=01|FILE_DESC=Periodic|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=B FILE_TYPE_NUM_02=FILE_TYPE=02|FILE_DESC=NCTO|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=M... (2 Replies)
Discussion started by: manas_ranjan
2 Replies

10. UNIX for Dummies Questions & Answers

how to use pattern match with grep

hi, i'm having problem like this. i wish to grep some keyword from certain files in one directory. let's say i have a lot of files in my directory with the name of file.1 file.2 file.3 ...... file.500 i only wish to grep the keyword from file.20, file.21, file.23 .... file.50 i tried... (5 Replies)
Discussion started by: rei
5 Replies
Login or Register to Ask a Question