I have some complicated logs that I try to parse. What I want is to find a string H, get column 5 from the line that matches, and then take all other lines where column 5 matches the line with the string H.
Example input
a b c d 31 1
a b c d 31 H
a b c d 31 2
a b c d 32 1
a b d d 32 2
a b c d 33 1
a b c d 33 H
a b c d 33 2
Expected output
a b c d 31 1
a b c d 31 H
a b c d 31 2
a b c d 33 1
a b c d 33 H
a b c d 33 2
So, I have been able to do so by making two separate scripts:
script1 | xargs -n1 | script2
script1 searches for string H, and then prints the fifth column.
cat logfile | grep 'H' | awk '{print $5}'
script2 then prints all other lines from the logfile where column 5 matches the output of the first script:
cat logfile | awk -v var="$1" '$5 == var'
I want to make the input file, the logfile, a variable, so that I can use $1 in the script, and then call script logfile
. But then I must merge the two scripts to one script, because both scripts parse the same logfile. What is, generally speaking, the right approach to do this? I say generally, because I am a newby to bash scripting.
The two problems that I encouter are, firstly: the $1 of the first script (which is the logfile) is different from the $1 in the second script (the number that is the output of the first script) that I pipe to awk. Secondly, I cannot find the equivalent of xargs -n1
for use within one bash script.