How do i Extract integer value from a string in Unix - grep

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

Related

extract part of grep line using regex

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

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)

Cut the Apostrophe (') with cut or awk

I'm trying to cut a line of asterisk output.
The complete line:
[2017-11-01 08:23:58] NOTICE[13443]: res_pjsip/pjsip_distributor.c:659 log_failed_request: Request 'INVITE' from '"66666" <sip:66666#192.168.1.248>' failed for '163.172.107.10:5070' (callid: f64a37f3cc5a88f4cd957ecb7b65a14f) - No matching endpoint found
I need to see only this 163.172.107.10 in my output
So my command is this:
cat test | grep endpoint | awk '{print $13}' | awk -F':' '{print $1}'
My output:
'163.172.107.10
But I can't get rid of the ' (Apostrophe)
I tried: cut -d '''
But that didn't work
Any suggestions?
If this line follows a similar patters as pointed above, then
try this
gawk -F"'" '{print $6}' | cut -d":" -f1
as in
echo "[2017-11-01 08:23:58] NOTICE[13443]: res_pjsip/pjsip_distributor.c:659 log_failed_request: Request 'INVITE' from '"66666" <sip:66666#192.168.1.248>' failed for '163.172.107.10:5070' (callid: f64a37f3cc5a88f4cd957ecb7b65a14f) - No matching endpoint found" | gawk -F"'" '{print $6}' | cut -d":" -f1
Please let me know if the below helps.
cat test | grep endpoint | awk '{print $13}' | awk -F':' '{print $1}' | tr -d "'"
If your Input_file is same as shown sample then following may help you in same.
awk '{sub(/.*failed for \047/,"");sub(/:.*/,"");print}' Input_file

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

Resources