Match grep output to values in file - grep

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

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

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"

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

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