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-*\
Related
I want to print the filename if only ALL the matches are present... on different lines
grep -l -w '10B\|01A\|gencode' */$a*filename.vcf
this prints out the filename, but not only if ALL three matches are present.
Would you consider to try awk? awk may solve it in following method,
awk '/10B/&&/01A/&&/gencode/{print FILENAME}' */$a*filename.vcf
try following, just edited your solution a bit.
grep -l '10B.*01A.*gencode' Input_file
With grep and its -P (Perl-Compatibility) option and positive lookahead regex (?=(regex)), to match patterns if in any order.
grep -lwP '(?=.*?10B)(?=.*?01A)(?=.*?gencode)' /path/to/infile
grep -l 'pattern1' files ... | xargs grep -l 'pattern2' | xargs grep -l 'pattern3'
From the grep manual:
-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. (-l is specified by POSIX.)
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'm trying to grep for a string that starts with "--"
for some reason it counted as special character, but even when trying to use -F then grep gives me bad syntax:
[root#pc-01 /]# grep -F --restore .
-bash: --restore: command not found
any tips?
Thanks.
Try following.
grep -F -- --restore filename
You can escape the first - :
Without escaping:
[root#TIAGO-TEST2 tmp]# echo '--aa --bb --cc' | grep -o '--b'
grep: option '--b' is ambiguous; possibilities: '--basic-regexp' '--binary' '--byte-offset' '--binary-files' '--before-context'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
Escaping:
[root#TIAGO-TEST2 tmp]# echo '--aa --bb --cc' | grep -o '\--b'
--b
I'm running this command on OS X to pull the logic board ID:
ioreg -l | grep board-id
which gives me this output:
| "board-id" = <"Mac-FC02E91DDD3FA6A4">
The only part I'm interested in is the "Mac-FC02E91DDD3FA6A4". Is there a way to filter the results from grep to only show me this part? OR is there a second step I could do to clean up the grep results?
Using awk you can do this
ioreg -l | awk -F\" '/board-id/ {print $4}
Mac-FC02E91DDD3FA6A4
This search for board-id, divide output by " and then print part 4
ioreg -l | grep "board-id" | cut -d \" -f 4
one way still with grep, try this line:
ioreg -l|grep -Po 'board-id".*<"\K[^"]*'
Is it possible to determine the number of times a particular word appears using grep
I tried the "-c" option but this returns the number of matching lines the particular word appears in
For example if I have a file with
some words and matchingWord and matchingWord
and then another matchingWord
running grep on this file for "matchingWord" with the "-c" option will only return 2 ...
note: this is the grep command line utility on a standard unix os
grep -o string file will return all matching occurrences of string. You can then do grep -o string file | wc -l to get the count you're looking for.
I think that using grep -i -o string file | wc -l should give you the correct output, what happens when you do grep -i -o string file on the file?
You can simply count words (-w) with wc program:
> echo "foo foo" | grep -o "foo" | wc -w
> 2