How to grep with regex lookahead - grep

I can't see what I'm missing in my grep command, can you?
http://regexr.com/5shri
echo "2021-05-09 15:38:56.888 T:1899877296 NOTICE: VideoPlayer::OpenFile:plugin://plugin.video.arteplussept/play/SHOW/069083-002-A" | grep -oE "\w+(?=\/play)/g" -
Expect: arteplussept

You need to
Use the PCRE regex engine, with -P option, not -E (which stands for POSIX ERE)
Remove /g, grep -o extracts all matches and there is no need to "embed" this modifier into the pattern
There is no need to escape /
So, you can just use
grep -oP '\w+(?=/play)'

Related

Why is \s not matching whitespace in my grep? [duplicate]

Both of the regexes below work In my case.
grep \s
grep ^[[:space:]]
However all those below fail. I tried both in git bash and putty.
grep ^\s
grep ^\s*
grep -E ^\s
grep -P ^\s
grep ^[\s]
grep ^(\s)
The last one even produces a syntax error.
If I try ^\s in debuggex it works.
Debuggex Demo
How do I find lines starting with whitespace characters with grep ? Do I have to use [[:space:]] ?
grep \s works for you because your input contains s. Here, you escape s and it matches the s, since it is not parsed as a whitespace matching regex escape. If you use grep ^\\s, you will match a string starting with whitespace since the \\ will be parsed as a literal \ char.
A better idea is to enable POSIX ERE syntax with -E and quote the pattern:
grep -E '^\s' <<< "$s"
See the online demo:
s=' word'
grep ^\\s <<< "$s"
# => word
grep -E '^\s' <<< "$s"
# => word

grep combine -f and -E

I want to combine the content of a patter file with a regular expressions, i.e. grep -E -f.
The input file has the format
2 List_of_anthropologists<!!>Q1279970
3 List_of_Governors_of_Alabama<!!>Q558677
2027476 12th_Dalai_Lama<!!>Q25240
etc..
and the pattern file has the format:
13th_Dalai_Lama
5th_Dalai_Lama
etc...
I can make it work by manually putting in the pattern "13th_Dali_Lama"
grep -E "^(\d*)(?:\t)13th_Dalai_Lama" input_file
But how to I combine the -f option so that 13th_Dalai_Lama is replace by the lines in the pattern file?
With GNU grep, GNU sed and bash:
grep -f <(sed 's/.*/\\b&\\b/' pattern_file) input_file

grep for variable pattern: echo "${foo}" | grep "'${bar}'"

I will need to grep one variable for a variable pattern.
Like so
foo="--test2"
bar="--test"
echo "${foo}" | grep "'${bar}'"
Unfortunately it is not working.
Any ideas about how to achieve this?
If you use , single quotes it will take it as literal string. Remove the single quotes. Then it will throw the error for -- in your string. For that use -e option for mention that is a pattern to match.
echo "${foo}" | grep -e "${bar}"
your pattern is leading with -, -e option is needed.
this line should work for your example:
echo "${foo}" | grep -e "${bar}"

repesent digits using regular expression

grep -w "ing_[0-9][0-9][0-9][0-9]"
The command mentioned above is working. But is there a short version of 4 digits?
This does not work:
grep -w "ing_[0-9]\+ {4}"
Grep by default use Basic Regular expressions. In BRE , you need to escape the curly braces so that it would consider the curly braces as repetition quantifier.
grep -w "ing_[0-9]\{4\}" file
Example:
$ echo 'ing_6786 says' | grep -w "ing_[0-9]{4}"
$ echo 'ing_6786 says' | grep -w "ing_[0-9]\{4\}"
ing_6786 says
If you are lucky and your grep supports modern (Perl) regular expressions, try -P argument
grep -wP "ing_[0-9]{4}"

Grep digits after match

I would like to grep digits inside a set of parentheses after a match.
Given foo.txt below,
foo: "32.1" bar: "42.0" misc: "52.3"
I want to extract the number after bar, 42.0.
The following line will match, but I'd like to extract the digit. I guess I could pipe the output back into grep looking for \d+.\d+, but is there a better way?
grep -o -P 'bar: "\d+.\d+"' foo.txt
One way is to use look ahead and look-behind assertions:
grep -o -P '(?<=bar: ")\d+.\d+(?=")'
Another is to use sed:
sed -e 's/.*bar: "\([[:digit:]]\+.[[:digit:]]\+\)".*/\1/'
You could use the below grep also,
$ echo 'foo: "32.1" bar: "42.0" misc: "52.3"' | grep -oP 'bar:\s+"\K[^"]*(?=")'
42.0

Resources