extract part of grep line using regex - grep

I'm using the following command to get java.home path
java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'
the command above returns
java.home = /usr/lib/jvm/java-11-openjdk-amd64
How can I get it to only return "/usr/lib/jvm/java-11-openjdk-amd64"

Just use awk to cut out the last field:
java -XshowSettings:properties -version 2>&1 >/dev/null | grep 'java.home' | awk '{print $NF}'
Or a little shorter:
java -XshowSettings:properties -version 2>&1 | awk '/java.home/ {print $NF}'

Related

dynamic exclusion of files through grep matching

I have a file source-push.sh which returns the list of files which I want to exclude from the results of find command.
It looks like this:
#!/usr/bin/env bash
find . -not \( -path './node_modules' -prune \) -name '*.js' | grep -vE $(echo $(./source-push.sh | xargs -I{} echo -n "{}|") | rev | cut -b2- | rev) | xargs -L1 standard --fix
find . -not \( -path './node_modules' -prune \) -name '*.css' | grep -vE $(echo $(./source-push.sh | xargs -I{} echo -n "{}|") | rev | cut -b2- | rev) | xargs -L1 stylelint --config stylelint.json
There are supposed to be a way to do the job better than that. Any suggestions?
Instead of:
... | grep -vE $(echo $(./source-push.sh | xargs -I{} echo -n "{}|") | rev | cut -b2- | rev ) | ...
you can use the POSIX options -F and -f:
... | grep -v -F -f <( ./source-push.sh ) | ...
-F tells grep that the patterns are fixed strings
(avoiding the problem that your original code would break if the patterns contain characters that are special to grep -E)
-f file tells grep to use a list of patterns from file
<( ... ) is a bash way to present output of a program as a file (named pipe)

grep -v under double quotes query

We have a portion of code which states,
"diff file1 file2 | /usr/bin/grep -v "#" | /usr/bin/grep ^\> | /usr/bin/awk '{print $3}' | /usr/bin/xargs mkdir"
The whole statement is enclosed in double quotes(is a requirement of the application syntax). When the application reaches this stage , it gives the grep error.
This statement works well on the command line. But through application, gives error for grep.
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
So not sure if it is first grep or second grep which is a problem.
Seems like a problem with double quotes. Try changing your first grep to /usr/bin/grep -v '#' and the second grep to /usr/bin/grep '^>'
You are using grep -v ^> and > means "redirect".
If you for example do:
grep ^>output
all the output will be stored in the file output.
So what you need to do is to quote ^> so that it is interpreted as the pattern you are looking for:
"diff file1 file2 | /usr/bin/grep -v "#" | /usr/bin/grep "^>" | /usr/bin/awk '{print $3}' | /usr/bin/xargs mkdir"
^ ^
By the way, note all your greps can be reduced like this:
diff file1 file2 | awk '/#/ || /^>/ {print $3}' | /usr/bin/xargs mkdir
^^^ ^^ ^^^^
either contains # | |
or starts with >

How do i Extract integer value from a string in Unix

when i type this command
/usr/local/afs7/bin/afs_paftools -a about.afs | grep TOTAL_DOCUMENTS
I get a result
TOTAL_DOCUMENTS = 74195
How i can extract the integer number(74195) after =
using grep command
One way is to use grep:
$ echo "TOTAL_DOCUMENTS = 74195" | grep -o '[0-9]\+'
74195
or since you know, that it's the last field, use awk:
$ echo "TOTAL_DOCUMENTS = 74195" | awk '{print $NF}'
74195
or just use awk for the lot:
your-command -a about.afs | awk '/TOTAL_DOCUMENTS/{print $NF}'
If there are no space:
TOTAL_DOCUMENTS=74195
Use this awk
echo "TOTAL_DOCUMENTS=74195" | awk -F= '{print $NF}'
74195

How to filter grep results

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[^"]*'

tail pipe grep pipe xmllint not working

I'm trying to get the below command working but no o/p is getting printed:
tail -f mylog.log | grep --line-buffered -Eo '<S:Envelope .+Envelope>' | xmllint --format --recover -
However, if I grep the same pattern from a file, and pipe it to xmllint, it works:
grep --line-buffered -Eo '<S:Envelope .+Envelope>' tmp.xml | xmllint --format --recover -
What am I missing in the first command?
Can you try this (untested):
tail -f mylog.log | grep -Eo '<S:Envelope .+Envelope>' | while read line; do
echo $line | xmllint --format --recover -
done
(that is under the hypothesis that xmllint does not find EOF and as such is still waiting for input)
Try something like this -
grep --line-buffered -Eo '<S:Envelope .+Envelope>' <(tail -f mylog.log) &1> xmllint --format --recover -

Resources