How to do it right: grep -ri '->'

cat a.txt | grep -ri '->' 

I want the grep lines with arrow text →. But in linux shell - means launching the option
and also > means the output of the string to the file.

So, after executing the above command, I got an error

 [ root@rs169 document_root]# cat a.txt | grep -ri '->' grep: invalid option -- '>' Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more information. 

Can someone tell how to do it right?

+4
source share
5 answers

Another way to do this is to use -- - this tells grep that you have finished using flags, and any - appearing after this should be interpreted literally.

 $ echo "->" | grep -- "->" -> 
+6
source

grep -ri '\->' a.txt should do

 [A]$ grep -ri '\->' a.txt 12 ->2 [A]$ cat a.txt 12 ->2 11 [A]$ sh -version GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. 
+4
source

Use the -e key to explicitly specify a pattern: grep -rie '->'

  -e PATTERN, --regexp=PATTERN Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-). (-e is specified by POSIX.) 
+3
source

On the Linux command line, you exit - Like this

 cat a.txt | grep -ri '\->' 

By the way, why do you need the -ri option? -> has no alphabet ( -i - for case insensitive searches), and cat a.txt will not create a list of recursive files / pipes that you need to search for

+2
source

Although you can avoid - in the template, the reason is that it does not work: grep leads to the idea that > (part of the template -> ) is the option passed to it, since grep does not have the > option, it complains.

The correct way is to tell grep to stop the pending options by adding -- :

 cat a.txt | grep -ri -- '->' 
+2
source

Source: https://habr.com/ru/post/1495916/


All Articles