0

I would like to search an exact word which contains the special char / like this word "/var/tmp". I found some example how to search whole word like this sed '/\b$word\b/g' But it works only on standard words, not contains meta. Any idea please?

INPUT>

 word="/var/tmp"
  cat /etc/mtab | grep $word
/dev/mapper/vgroot-lvvar /tmp/var/tmp ext4 rw,seclabel,relatime 0 0
/dev/mapper/vgroot-lv_var_tmp /var/tmp xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
/dev/mapper/vgroot-lv_var_tmp /tmp/var/tmp xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0

OUTPUT is working only with exact example, but not with universal variable $word

cat /etc/mtab | grep $word | sed '/\b\/var\/tmp\b/g'
/dev/mapper/vgroot-lv_var_tmp /var/tmp xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0

Not working output with variable, filter not working here

cat /etc/mtab | grep $word | sed '/\b\$word\b/g'
/dev/mapper/vgroot-lvvar /tmp/var/tmp ext4 rw,seclabel,relatime 0 0
/dev/mapper/vgroot-lv_var_tmp /var/tmp xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
/dev/mapper/vgroot-lv_var_tmp /tmp/var/tmp xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
andrew
  • 459
  • 5
  • 21
  • You can't `sed '/\b\$word\b/g'` because the single quotes keep the variable expansion from working - and even if you used double quotes it still won't work because you are escaping the dollar sign. Also, if `$word` is going to contain `/` then you should use a different delimiter for sed. Something like `sed "#\b\$word\b#g"` You might also want to use double quotes around the parameter to grep - it works now but what if `$word` contained a semicolon, space, ampersand or some other character special to the shell? – Jerry Jeremiah Sep 21 '21 at 22:27
  • I rolled back your new question. Accept the answer you got and ask a new question if you can't figure it out; but really, search before asking - these are common beginner questions. – tripleee Sep 22 '21 at 06:43
  • Tangentially, avoid the [useless `cat`.](https://stackoverflow.com/questions/11710552/useless-use-of-cat) – tripleee Sep 22 '21 at 08:24
  • @andrew: I think that you should explain to us what you understand by "word with metacharacters"? Which characters are allowed to be a part of the word? Which characters are the delimiters of the word? Etc... – virolino Sep 22 '21 at 16:12
  • Sorry for misunderstandings, I was meaning special character "/". Delimiuter is space. – andrew Sep 23 '21 at 14:17

1 Answers1

0

You can use sed with a different delimiter for it. To use it in address position you need to escape the very first one - \%.

Also, as your word can start(and probable end) with /(non word character) you need to handle the beginning of words manually, not with \b. Otherwise you will not have a match.

Below is a working example:

$  cat txt
 asd fasd /asd fas
asd asd /asd/tmp/ asdf
/asd/tmp/ asdf
asdf
sadf asd asd/tmp

$  echo $word
/asd/tmp/

$  sed -nE "\%(^|\s)$word(\s|$)%p" txt
asd asd /asd/tmp/ asdf
/asd/tmp/ asdf

-n - tells sed do not print all lines
-E - to work with extended regexp syntax
\% - delimiter which shouldn't appear in $word
(^|\s) - match the very beginning of a line or space
(\s|$) - match a space or the end of line
p - tells sed to print matched lines