Cut the Apostrophe (') with cut or awk - grep

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

Related

How to correctly format df pipe through genmon?

I am having trouble with command output formatting.
In terminal this works nicely:
df | grep sda1 | head -c33 | tail -c7 | tr -d " "
In genmon, I get only numbers such as "1145944":
SDAFREE=$(df | grep sda1 | head -c33 | tail -c7 | tr -d " ")
echo="$SDAFREE"
How do I print that command's output through genmon to xfce panel correctly (same as in terminal)?
Thank you.
I have the same issue with every command with a pipe. As a workaround I put the command in a executable script and run the script in genmon.
BTW:
if you want just one value of a table, you can use awk instead of head, tail and tr:
df | awk '/sda1/ {print $4}'

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

Parsing the output of /proc/net/dev with awk and dismissing the first two lines

This command gives me the following output:
cat /proc/net/dev | awk '{print $1}'
Inter-|
face
eth0:
lo:
wlan0:
Is there a way to dismiss the lines inter-|, face so i can get only the names of the interfaces?
Tweaking your awk a little bit:
awk 'NR>2{print $1}' /proc/net/dev
tail -n +3 /proc/net/dev | awk...
tail -n {+whatever} (note the plus sign) can be used to dump files starting from the nth line.
There are many ways of doing this. If you just need it work for that specific case I'd do something simple like this:
cat /proc/net/dev | awk '{print $1}' | sed -e '1,2d'
Sed '1,2d' just means delete lines 1 and 2.
In addition to the other answers:
just in case you don't want to use awk with tail, you can do also this:
tail -n +3 /proc/net/dev | cut -d':' -f1

Resources