How to filter grep results - grep

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

Related

grep for pattern with special character and output only matched string

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-*\

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

How come I don't get all the lines with grep and grep -v

Why does grep -v POLYGON remove many more lines than those matching grep POLYGON?
$ cat BOUNDARIES3D_LV03.nt | grep -v POLYGON | wc
249 782 137001
$ cat BOUNDARIES3D_LV03.nt | grep POLYGON | wc
2441 2753697 51833677
$ cat BOUNDARIES3D_LV03.nt | wc
73078 2975809 91746795
Is this a bug in grep (using: grep (GNU grep) 2.23) or am I misunderstanding something?
Update
It seems that grep aborts at the first matching line containing an invalid character.
The problem was that grep aborts at the first line containing a byte sequence that doesn't evaluate to a character in the current encoding. The following resolved the issue for me:
export LC_ALL="en_US.UTF-8"

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

Match grep output to values in file

I have a file name clfile.me that looks like this;
44433430,"FALSE"
33095934,"TRUE"
41549968,"TRUE"
37945528,"FALSE"
18284764,"FALSE"
15007934,"FALSE"
The number is AIX PID. I have a command that will match the PIDs to a running process.
while read p; do
ps -ef | grep $p | grep 'myproram' | grep -v grep | awk "{ print \$2 }" >> clout.me
done < clfile.me
THe above works but only shows me the PID that matched from the grep command. I want to be able to see the matching PID and the TRUE or FALSE value from the original file. I guess I am asking how I filter the original file by PIDs that match my grep command.
Any thoughts?
Thanks
Chris
Took me a while, but I have it!
cat /dev/null > clout.me
while read p; do
x=$(awk '{ print $1 }')
ps -ef | grep x | grep 'myprogram' | grep -v grep | awk "{ print \$2 }" >> clout.me
done < clfile.me
awk 'FNR==NR{A[$1]=1;next} A[$1]' clout.me clfile.me

Resources