How to suppress patterns being treated as options in grep - linux

In grep, when I try to find a pattern in the man page, the pattern is treated as the option of grep if it starts with -. For example, I find someone is using uname -r and I wonder what does -r means for uname.
uname --help | grep '-r'
But I got the following error,
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
Seems as if -r is treated as an option for grep.

There are two ways:
If supported by your version, use -- to stop argument processing.
uname --help | grep -- '-r'
Use the -e option to specify the pattern. Since it expects an argument,
grep will not treat the next string as a new option just because it starts with a hyphen.
uname --help | grep -e '-r'

As described in the man page for grep:
-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.)
uname --help | grep -e -r does what is expected.

Backslash also seems to help:
uname --help | grep "\-r"
uname --help | grep '\-r'

Related

Grep filter not stop at first match

I am trying to use
grep -o "javascript:add2BasketProd.*?jpg"
to extract string from
javascript:add2BasketProd('xKonfGFJsakj', 'Tattoo-Bubble-Gum-7cm-Bubble-Gum.jpg')Funny-Glasses-and-Teeth-Toy-Candy-TC-747-.jpg
but it would give no output. So I changed the code to the following but the output I get is entire string and not until the first match of jpg.
grep -o "javascript:add2BasketProd.*\?jpg"
My expected output is:
javascript:add2BasketProd('xKonfGFJsakj', 'Tattoo-Bubble-Gum-7cm-Bubble-Gum.jpg
Can anyone suggest a solution?
grep uses POSIX style regular expression which are always greedy. If your grep supports the -P flag, you can use it to enable perl style regex which do support non-greedy matches:
grep -oP "javascript:add2BasketProd.*?jpg"
The GNU grep, which is used on linux, supports -P. The Mac OSX grep does not.
You can use egrep for advanced regex capabilities:
s='javascript:add2BasketProd('xKonfGFJsakj', 'Tattoo-Bubble-Gum-7cm-Bubble-Gum.jpg')Funny-Glasses-and-Teeth-Toy-Candy-TC-747-.jpg'
egrep -o "javascript:add2BasketProd.*?jpg" <<< "$s"
javascript:add2BasketProd(xKonfGFJsakj, Tattoo-Bubble-Gum-7cm-Bubble-Gum.jpg

grep with negative pattern

I am looking for a way to grep a file for a specific pattern with negative pattern in it.
I have a log file witch reports units version and I want to see if there is a unit witch report version other then 26.
The closest I could get is :
cat my.log | grep -i -e "version=0x[^2][^6]"
The above return a line contain "version=0x13" but not return a line contain "version=0x23"
Is there a way to tell grep to do so ?
Thanks.
Interpret the pattern as a perl regular expression using the -P switch:
grep -iP 'version=0x(?!26)\d\d' my.log
grep -i "version=0x[0-9]\\+" my.log | grep -iv "version=0x26"

Grep not as a regular expression

I need to search for a PHP variable $someVar. However, Grep thinks that I am trying to run a regex and is complaining:
$ grep -ir "Something Here" * | grep $someVar
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
$ grep -ir "Something Here" * | grep "$someVar"
<<Here it returns all rows with "someVar", not only those with "$someVar">>
I don't see an option for telling grep not to interpret the string as a regex, but to include the $ as just another string character.
Use fgrep (deprecated), grep -F or grep --fixed-strings, to make it treat the pattern as a list of fixed strings, instead of a regex.
For reference, the documentation mentions (excerpts):
-F --fixed-strings Interpret the pattern as a list of fixed
strings (instead of regular expressions), separated by newlines, any
of which is to be matched. (-F is specified by POSIX.)
fgrep is the same as grep -F. Direct invocation as fgrep is
deprecated, but is provided to allow historical applications that rely
on them to run unmodified.
For the complete reference, check:
https://www.gnu.org/savannah-checkouts/gnu/grep/manual/grep.html
grep -F is a standard way to tell grep to interpret argument as a fixed string, not a pattern.
You have to tell grep you use a fixed-string, instead of a pattern, using '-F' :
grep -ir "Something Here" * | grep -F \$somevar
In this question, the main issue is not about grep interpreting $ as a regex. It's about the shell substituting $someVar with the value of the environment variable someVar, likely the empty string.
So in the first example, it's like calling grep without any argument, and that's why it gives you a usage output. The second example should not return all rows containing someVar but all lines, because the empty string is in all lines.
To tell the shell to not substitute, you have to use '$someVar' or \$someVar. Then you'll have to deal with the grep interpretation of the $ character, hence the grep -F option given in many other answers.
So one valid answer would be:
grep -ir "Something Here" * | grep '$someVar'
+1 for the -F option, it shall be the accepted answer.
Also, I had a "strange" behaviour while searching for the -I.. pattern in my files, as the -I was considered as an option of grep ; to avoid such kind of errors, we can explicitly specify the end of the arguments of the command using --.
Example:
grep -HnrF -- <pattern> <files>
Hope that'll help someone.
Escape the $ by putting a \ in front of it.

Grep Search all files in directory for string1 AND string2

How can I make use of grep in cygwin to find all files that contain BOTH words.
This is what I use to search all files in a directory recursively for one word:
grep -r "db-connect.php" .
How can I extend the above to look for files that contain both "db-connect.php" AND "version".
I tried this: grep -r "db-connect.php\|version" . but this is an OR i.e. it gets file that contain one or the other.
Thanks all for any help
grep -r db-connect.php . | grep version
If you want to grep for several strings in a file which have different lines, use the following command:
grep -rl expr1 | xargs grep -l expr2 | xargs grep -l expr3
This will give you a list of files that contain expr1, expr2, and expr3.
Note that if any of the file names in the directory contains spaces, these files will produce errors. This can be fixed by adding -0 I think to grep and xargs.
grep "db-connect.php" * | cut -d: -f1 | xargs grep "version"
I didn't try it in recursive mode but it should be the same.
To and together multiple searches, use multiple lookahead assertions, one per thing looked for apart from the last one:
instead of writing
grep -P A * | grep B
you write
grep -P '(?=.*A)B' *
grep -Pr '(?=.*db-connect\.php)version' .
Don’t write
grep -P 'A.*B|B.*A' *
because that fails on overlaps, whereas the (?=…)(?=…) technique does not.
You can also add in NOT operators as well. To search for lines that don’t match X, you normally of course use -v on the command line. But you can’t do that if it is part of a larger pattern. When it is, you add (?=(?!X).)*$) to the pattern to exclude anything with X in it.
So imagine you want to match lines with all three of A, B, and then either of C or D, but which don’t have X or Y in them. All you need is this:
grep -P '(?=^.*A)(?=^.*B)(?=^(?:(?!X).)*$)(?=^(?:(?!Y).)*$)C|D' *
In some shells and in some settings. you’ll have to escape the ! if it’s your history-substitution character.
There, isn’t that pretty cool?
In my cygwin the given answers didn't work, but the following did:
grep -l firststring `grep -r -l secondstring . `
Do you mean "string1" and "string2" on the same line?
grep 'string1.*string2'
On the same line but in indeterminate order?
grep '(string1.*string2)|(string2.*string1)'
Or both strings must appear in the file anywhere?
grep -e string1 -e string2
The uses PCRE (Perl-Compatible Regular Expressions) with multiline matching and returns the filenames of files that contain both strings (AND rather than OR).
grep -Plr '(?m)db-connect\.php(.*\n)*version|version(.*\n)*db-connect\.php' .
Why to stick to only grep:
perl -lne 'print if(/db-connect.php/&/version/)' *

grep command working in testdir but not in "real" directory

I just thought I had found my solution because the command works in my test directory.
grep -H -e 'author="[^"].*' *.xml | cut -d: -f1 | xargs -I '{}' mv {} mydir/.
But using the command in the non-test-direcory the command did not work:
This is the error message:
grep: unknown option -- O
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
Not even this worked:
$ grep -H author *.xml
or this:
$ grep -H 'author' *.xml
(same error message)
I suspect it has some relation to the file names or the amount of files.
I have almost 3000 files in the non-test-directory and only 20 in my test directory.
In both directories almost all file names contain spaces and " - ".
Some more info:
I'm using Cygwin.
I am not allowed to change the filenames
Try this (updated):
grep -HlZ 'author="[^"].*' -- *.xml | xargs -0 -I {} mv -- {} mydir/
EXPLANATION (updated)
In your "real" directory you have a file with name starting with -O.
Your shell expands the file list *.xml and grep takes your - starting filename as an option (not valid). Same thing happens with mv. As explained in the Common options section of info coreutils, you can use -- to delimit the option list. What comes after -- is considered as an operand, not an option.
Using the -l (lowercase L) option, grep outputs only the filename of matching files, so you don't need to use cut.
To correctly handle every strange filename, you have to use the pair -Z in grep and -0 in xargs.
No need to use -e because your pattern does not begin with -.
Hope this will help!

Resources