tail pipe grep pipe xmllint not working - grep

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 -

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)

trying to grep '--string' fails

I'm trying to grep for a string that starts with "--"
for some reason it counted as special character, but even when trying to use -F then grep gives me bad syntax:
[root#pc-01 /]# grep -F --restore .
-bash: --restore: command not found
any tips?
Thanks.
Try following.
grep -F -- --restore filename
You can escape the first - :
Without escaping:
[root#TIAGO-TEST2 tmp]# echo '--aa --bb --cc' | grep -o '--b'
grep: option '--b' is ambiguous; possibilities: '--basic-regexp' '--binary' '--byte-offset' '--binary-files' '--before-context'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
Escaping:
[root#TIAGO-TEST2 tmp]# echo '--aa --bb --cc' | grep -o '\--b'
--b

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

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