2

I would like to grep the following string in exact order.

x*/\*y\*/\*z\*/\*w\*

how can I do it?

grep x*/\*y\*/\*z\*/\*w\* <file> -> does not work

Example input

xvg/cyv/dgzfdre/rwt 
avg/cyv/dgzfdre/rwt 
x/y/z/w 

Desired output

xvg/cyv/dgzfdre/rwt 
x/y/z/w 
  • Are those stars part of the string? Then you need to escape them by backslashes as they have a special meaning in search patterns – Philippos Mar 23 '17 at 21:37
1

This achieves what you appear to require.

$ cat testfile
xvg/cyv/dgzfdre/rwt
avg/cyv/dgzfdre/rwt
x/y/z/w
$ grep 'x.*/.*y.*/.*z.*/.*w' testfile
xvg/cyv/dgzfdre/rwt
x/y/z/w
$

Your grep x*/\*y\*/\*z\*/\*w\* <file> does not work as it's looking for zero or more x characters followed by a /, followed by a * character, followed immediately by a y character [etc].

  • 1
    Thx! it worked. – Dor Mar 24 '17 at 8:34
0

*s are magic characters in regular expressions that mean 'zero or more of the preceding item'. You need to escape them:

grep 'x\*/\*y\*/\*z\*/\*w\*' /path/to/file
  • did not work :( I created a file with the following stalemates xvg/cyv/dgzfdre/rwt avg/cyv/dgzfdre/rwt x/y/z/w I accept it to grep line #1 and #3 and it did not catch anything – Dor Mar 23 '17 at 21:49
  • @Dor So actually the stars are not part of the string, but wildcards. But beware: Grep uses regex patterns, unlike the pattern matching in the shell, so if you want to match any string, use .*. The point is for "any char", the star for "the preceding atom repeated". Read man 7 regex – Philippos Mar 23 '17 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.