Match specific word with grep -e - grep

I am trying to use grep to print only lines that start with a specific pattern. Here is an example
$SERVER_IP = 2.2.2.2
$SERVER_IP_PORT = 1111
$SERVER_IP_XXX = blablabla
I want grep to print only SERVER_IP = 2.2.2.2 and not the other three lines.
I tried the command below but it did not work
grep -e "^\s*\$SERVER_IP$"
If I try:
grep -e "^\s*\$SERVER_IP"
grep will print all three lines
How can I accomplish this using grep -e or egrep? Thank you

grep -e "^\s*\$SERVER_IP\>"
The \> means "word-boundary", or "place where word characters meet non-word characters."

Use grep -e '^\$SERVER_IP =' to match any line that starts with $SERVER_IP =

If you have awk, you can do:
awk '$1=="$SERVER_IP"' file
$SERVER_IP = 2.2.2.2
The == makes it match only while field 1 is exact $SERVER_IP

Related

How to display two substring from a line?

How can we find two substrings within a line in particular order using grep?
For example:
grep -c "word1" | grep -r "word2" logs
gives if string has both word1 and word2. I am looking for string which has "... word1.... word2..."
Try a regex in grep like grep -E "word1.*word2"
$ echo -e 'both word1 and word2. \nI hich\n has "... word1.... word2..."' | grep -E "word1.*word2"
both word1 and word2.
has "... word1.... word2..."
You may need a better regex to match exactly the words, but that is not your question.

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 find pattern but print another line

I want to match all cyrillic characters, but print the ID to file. For example:
Author: Doe, John
Title: Оптимизация ресурсного потенциала промышленности города с учетом его конкурентных преимуществ
ID: 1234567
My current approach is to grep for cyrillic characters:
grep -i -r --include=*{rdf,redif,rdf~} --color="auto" -P -n '[\x{0400}-\x{04FF}]' > cyrillic.txt
How can I just print the ID line to a file and not the matching line?
Use -A1 option if the ID: line is right after the matching pattern. Then pipe it to another grep to get the line with ID:.
grep -A1 -i -r --include=*{rdf,redif,rdf~} --color="auto" -P -n '[\x{0400}-\x{04FF}]' \
| grep 'ID: ' > cryllic.txt
Use grep flag h - to suppress output file names - you'll have output like:
4:string with matching pattern
5:string with matching pattern
7:string with matching pattern
Now you can pipe this output into awk and print only fist column, which is matching string number:
{your_grep} | awk -F ':' '{print $1}' > cyrillic.txt

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}"

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