GREP "--prefix" as a pattern - grep

When I use grep -nrF '--prefix' * I get unrecognized option --prefix.
Is there any way to find this speical chracter such as -- using grep?

Cartman is right that the general way is to use -- but a grep specific solution is to use -e:
grep -nrFe '--prefix' *
From man 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.)

Use -- to tell grep that the rest is not a command line option
grep -nrF -- '--prefix' *

Related

dot in grep command being used as regex

I'm trying to understand if bash is doing something with the string before passing it to grep or if grep uses basic regex searching by default. The man page and other answers don't really clarify
ss -an | grep "8.02"
u_dgr UNCONN 0 0 * 820284002 * 820284001
u_str ESTAB 0 0 * 820283949 * 820287456
It looks like the . is being used in a regex fashion to match a single char. However, I would only expect this to happen when using grep -e or grep -E. If bash was intercepting the string I would expect special shell chars to be intercepted first such as * or ?.
The man entry states I am using GNU grep 3.1
Looks like I have immediately found the answer after RTFMing a little closer
-G, --basic-regexp
Interpret PATTERN as a basic regular expression (BRE, see below). This is the default.
"This is the default" - I assume means this is the default behaviour if no flags are passed?

Find a string between two characters with grep

I have found on this answer the regex to find a string between two characters. In my case I want to find every pattern between ‘ and ’. Here's the regex :
(?<=‘)(.*?)(?=’)
Indeed, it works when I try it on https://regex101.com/.
The thing is I want to use it with grep but it doesn't work :
grep -E '(?<=‘)(.*?)(?=’)' file
Is there anything missing ?
Those are positive look-ahead and look behind assertions. You need to enable it using PCRE(Perl Compatible Regex) and perhaps its better to get only matching part using -o option in GNU grep:
grep -oP '(?<=‘)(.*?)(?=’)' file

grep - List out items ending with '(' from the file

I have a list and need to extract the below patterns
funcName(
funcNameOther(
all function names ending with '( succeeded by a newline'
egrep -o '.*\($' does the trick for me, if that's what you're looking after.
grep '($' file
If that doesn't do what you want, provide some more useful sample input and expected output.

How to use grep to search for an exact word match in TextWrangler

There is a possibility to search using grep in TextWrangler
I want to find and replace the following word: bauvol, but not bauvolumen.
I tried typing ^bauvol$ into the search field but that didn't do the trick, it didn't find anything, although the word is clearly there.
I think it's because, in grep, the ^and $signify start and end of line, not a word?!
You want to use \b as word boundaries, as #gromi08 said:
\bbauvol\b
If you want to copy any portion of this word (so you can replace it, modify it, change the case, etc.) it is usually best to wrap it in ( and ) braces so you can reference them in the Replace box:
Find:
(\bbauvol\b)
Replace:
<some_tag>\1</some_tag>
Did you have anything specific you were trying to do with the result once you found it (cut it, duplicate it, etc.)?
Use the -w option of grep (see grep man-page.
This option searches for the expression as a word.
Therefore the command will be:
cat file.txt | grep -w bauvol
And yes, ^ and $ are for start and end of line.

How to filter using grep on a selected word

grep (GNU grep) 2.14
Hello,
I have a log file that I want to filter on a selected word. However, it tends to filter on many for example.
tail -f gateway-* | grep "P_SIP:N_iptB1T1"
This will also find words like this:
"P_SIP:N_iptB1T10"
"P_SIP:N_iptB1T11"
"P_SIP:N_iptB1T12"
etc
However, I don't want to display anything after the 1. grep is picking up 11, 12, 13, etc.
Many thanks for any suggestions,
You can restrict the word to end at 1:
tail -f gateway-* | grep "P_SIP:N_iptB1T1\>"
This will work assuming that you have a matching case which is only "P_SIP:N_iptB1T1".
But if you want to extract from P_SIP:N_iptB1T1x, and display only once, then you need to restrict to show only first match.
grep -o "P_SIP:N_iptB1T1"
-o, --only-matching show only the part of a line matching PATTERN
More info
At least two approaches can be tried:
grep -w pattern matches for full words. Seems to work for this case too, even though the pattern has punctuation.
grep pattern -m 1 to restrict the output to first match. (Also doable with grep xxx | head -1)
If the lines contains the quotes as in your example, just use the -E option in grep and match the closing quote with \". For example:
grep -E "P_SIP:N_iptB1T1\"" file
If these quotes aren't in the text file, and there's blank spaces or endlines after the word, you can match these too:
# The word is followed by one or more blanks
grep -E "P_SIP:N_iptB1T1\s+" file
# Match lines ending with the interesting word
grep -E "P_SIP:N_iptB1T1$" file

Resources