Question

I am using Solaris 5.10 and working on Bash Shell. I have a shell script in which the following statement not working

ssh 172.18.16.55 "df -h" |grep -w /data1

When I replace /data1 with data1 (i.e removed "/"), it is working correctly.

I have another statement like below which is working fine.

ssh 172.18.16.55 "df -h" |grep -w /dev/dsk/c1t0d0s0

According to my understanding, first occurrence of "/" (forward slash) is not getting recognized by the shell script. I want to know what is the significance of "/" in shell script? below are my outputs.

root@P020XIDCNF # ssh 172.18.16.55 "df -h" |grep '/data1\>'
/dev/md/dsk/d50         84G    59G    24G    71%    /data1

root@P020XIDCNF # ssh 172.18.16.55 "df -h" |grep -w /data1

does not print anything.


root@P020XIDCNF # ssh 172.18.16.55 "df -h" |grep /data1
/dev/md/dsk/d50         84G    59G    24G    71%    /data1
Was it helpful?

Solution

grep's -w option tells to match the pattern as a "word", i.e., with a "word boundary" before and after it. I think what's happening is that grep's idea of a word boundary doesn't match what you're expecting.

You can use \< and \> to match word boundaries explicitly.

Here are some examples run on a Solaris 9 system:

% echo /dev/md/dsk/d50 84G 59G 25G 71% /data1 | grep -w /data1
% echo /dev/md/dsk/d50 84G 59G 25G 71% /data1 | grep '\</data1\>'
% echo /dev/md/dsk/d50 84G 59G 25G 71% /data1 | grep '/data1\>'  
/dev/md/dsk/d50 84G 59G 25G 71% /data1
% 

Apparently Solaris grep doesn't consider the the boundary between a space and a / character to be a word boundary.

I suggest running man grep, and probably following some of the "See also" references, to see just how this is documented. (I tried to do that myself, but the Solaris system I have access to is having problems at the moment.)

If you want to use this command as part of a reasonably portable script, you need to be aware that this is an area where GNU grep behaves differently. I get the following on my Linux system with GNU grep:

% echo /dev/md/dsk/d50 84G 59G 25G 71% /data1 | grep -w /data1
/dev/md/dsk/d50 84G 59G 25G 71% /data1
% echo /dev/md/dsk/d50 84G 59G 25G 71% /data1 | grep '\</data1\>'
% echo /dev/md/dsk/d50 84G 59G 25G 71% /data1 | grep '/data1\>'  
/dev/md/dsk/d50 84G 59G 25G 71% /data1
% 

Note that your question would have been easier to answer if you had shown us the exact line that you expected to be matched.

OTHER TIPS

Are you sure that the output returned by df -h actually contains the string /data1? grep will not print anything if the string is not found.

EDIT In order to make the answer easier to find, his problem was that grep did not consider /data1 a word.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top