Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

shell

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

I want to grep lines with text arrow ->. But in linux shell - means starts of an option
and > means pipe output to a file.

So after doing command given above ,I got 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 correctly?

like image 655
truease.com Avatar asked Aug 08 '13 09:08

truease.com


People also ask

How do I use the grep command?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

How do I grep exact match?

The grep command prints entire lines when it finds a match in a file. To print only those lines that completely match the search string, add the -x option. The output shows only the lines with the exact match.

How do you grep 5 lines before and after?

For BSD or GNU grep you can use -B num to set how many lines before the match and -A num for the number of lines after the match. If you want the same number of lines before and after you can use -C num . This will show 3 lines before and 3 lines after. Save this answer.

How do you grep with strings?

To find a pattern that is more than one word long, enclose the string with single or double quotation marks. The grep command can search for a string in groups of files. When it finds a pattern that matches in more than one file, it prints the name of the file, followed by a colon, then the line matching the pattern.


1 Answers

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

$ echo "->" | grep -- "->"
->
like image 86
Yossarian Avatar answered Sep 26 '22 00:09

Yossarian