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}"
Related
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.
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)'
Team,
I want to grep for a substring container - and then only output that string and not whole line. how can i? I know i can awk on space and pull using $ but want to know how to do in grep?
echo $test_pods_info | grep -F 'test-'
output
test-78ac951e-89a6-4199-87a4-db8a1b8b054f export-9b55f0d5-071d-431-1d2ux0-avexport-xavierisp-sjc4--a4dd85-102 1/1 Running 0 19h
expected output
test-78ac951e-89a6-4199-87a4-db8a1b8b054f
awk is more suitable for this as you want to get first field in a matching line:
awk '/test-/{print $1}' <<< "$taxIncluded"
test-78ac951e-89a6-4199-87a4-db8a1b8b054f
If you really want to use grep then this might be what you're looking for:
grep -o 'test-\S*' <<< "$taxIncluded"
or:
grep -o 'test-[^[:space:]]*' <<< "$taxIncluded"
Try
echo $test_pods_info | grep -o 'test-'
the -o option is:
show[ing] only the part of a line matching PATTERN
according to grep --help. Of course, this will only print test-, so you'll need to rework your regex:
grep -oE '(test).*[[:space:]]\b'
Figured it out..
echo $test_pods_info | grep -o "\test-\w*-\w*\-\w*\-\w*\-\w*"
outoput
test-78ac951e-89a6-4199-87a4-db8a1b8b054f
but i wish there is simple way. like \test-*\
I am trying to search a text in some of the xibs in my project and replace the found text with some other text. I am using below mentioned command to perform the mentioned action but it is saying
"grep: warning: recursive search of stdin" and going to infinite waiting state.
grep -i -r --include=*.xib “$MSAwLjMxMTU4NDA0NDMgMC4wOTczNjMxNzM3NQA" myProjectPath | sort | uniq | xargs perl -e “s/$MSAwLjMxMTU4NDA0NDMgMC4wOTczNjMxNzM3NQA/$MC4xNTI5NDExODIzIDAuODA3ODQzMjA4MyAwLjE4MDM5MjE2MQA/" -pi
Please let me know where i am going wrong.
Thanx in advance.
The shell is expanding $MSAwLjMxMTU4NDA0NDMgMC4wOTczNjMxNzM3NQAas a variable and grep is producing output in the form "file: ", which sort | uniq is not correcting.
grep -l -i -r --include=*.xib '\$MSAwLjMxMTU4NDA0NDMgMC4wOTczNjMxNzM3NQA' myProjectPath | xargs perl -pi -e 's/\$MSAwLjMxMTU4NDA0NDMgMC4wOTczNjMxNzM3NQA/\$MC4xNTI5NDExODIzIDAuODA3ODQzMjA4MyAwLjE4MDM5MjE2MQA/' "$file"
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