Operating System - HP-UX
1833742 Members
2522 Online
110063 Solutions
New Discussion

Re: what is the problem with grep -x here.

 
SOLVED
Go to solution
Prasad Joshi
Regular Advisor

what is the problem with grep -x here.

I wanted to know which filesystem has inode 2.
So i tried this command and got the answer.

bash-2.02# ls -i / | grep 2
128674 .osuuid
6521 .ssh
87726 bin
125345 core
261 dev
125464 mnt
6525 nfsmount
283 opt
135620 performix.pl
2 stand
138942 state
136697 tc2.sh
2671 temp
12 tmp
bash-2.02# ls -i / | grep -x 2

But, why grep -x is not giving me the exact answer.

Thanks & regards,
Prasad
13 REPLIES 13
Peter Godron
Honored Contributor

Re: what is the problem with grep -x here.

Prasad,
you have to 'anchor' the grep:
ls -i / | grep '^2 '
Chan 007
Honored Contributor

Re: what is the problem with grep -x here.

Prasad,

Use ls -il |grep -x 2

Chan
Peter Godron
Honored Contributor

Re: what is the problem with grep -x here.

Prasad,
sorry, my previous answer only works if the '2 ' is right at the start of the line.
Perhaps better:
ls -i1 / | grep ' 2 '

No points for this part of the answer, please.
Prasad Joshi
Regular Advisor

Re: what is the problem with grep -x here.

Hi Peter,

I tried this
bash-2.02# ls -i / | grep -x '^2'

But, this does not give the expected answer.

Thanks
Prasad Joshi
Regular Advisor

Re: what is the problem with grep -x here.

All these does not work.

bash-2.02# ls -i / | grep -x 2
bash-2.02# ls -i / | grep -x "^2"
bash-2.02# ls -i / | grep -x '^2'
bash-2.02# ls -i / | grep -x '2'
bash-2.02# ls -i / | grep -x ' 2 '
bash-2.02# ls -il / | grep -x ' 2 '
bash-2.02# ls -il / | grep -x 2
Peter Godron
Honored Contributor

Re: what is the problem with grep -x here.

Prasad,
ths -x only works if the WHOLE line matches.
grep 2 return any line that has the number 2 in it. Doing the ' 2 ' means, only return the lines with space-2-space.
man grep
Robert-Jan Goossens_1
Honored Contributor

Re: what is the problem with grep -x here.

grep -x

(eXact) Matches are recognized only when the
entire input line matches the fixed string orregular expression.

the entire input line of 2 includes stand.

Robert-Jan

Ninad_1
Honored Contributor

Re: what is the problem with grep -x here.

Prasad,

The answer is actually very simple.
Please read the man page of grep carefully - to understand the -x options.
-x is used to match the exact line, thus when you are just gicing 2 and there are so many other characters/words on the same line - so the exact match for 2 fails.
Just to understand better do a small experiment.
so a grep -il | grep -x 2
Now once you get the output do
grep -il | grep -x "the whole line copy pasted"

Paste any of the lines - the whole line in above command , including starting spaces etc.
Ans you will understand,

Regards,
Ninad
Chan 007
Honored Contributor

Re: what is the problem with grep -x here.

Hi,

use the below

ls -il / |awk '{print $1}'|grep -x 2

testos:/opt/omni/etc # ls -il / |awk '{print $1}'|grep -x 2
2
2
2
2
2
2
2


Chan
Prasad Joshi
Regular Advisor

Re: what is the problem with grep -x here.

Now, my problem is changed.

How to find the file system which has some specific inode, 2 in this case.

Thanks a lot.
Ninad_1
Honored Contributor
Solution

Re: what is the problem with grep -x here.

Prasad,

Use the find command

find /pathname -inum 2

This should give what you want.

regards,
Ninad
Prasad Joshi
Regular Advisor

Re: what is the problem with grep -x here.

Thanks a lot for all your help.

This is very good learning experience.
Thanks again.
Robert-Jan Goossens_1
Honored Contributor

Re: what is the problem with grep -x here.

all mountpoints have a inum 2

# cat /etc/fstab | awk '{ print $2 }' | while read LINE
do
ls -ild $LINE
done

Regards,
Robert-Jan