linux - Take string output from command, and search for that string in a file? -
i wondering how use output command pattern argument of grep? in particular, have command i've cooked (with question):
grep pattern file.txt | awk 'nr == 1 {line = $0; min = $5} nr > 1 && $5 < min {line = $0; min = $5} end{print $5}' that search file.txt line 5th column has lowest value, print value. however, want redirect grep again, search file.txt again line, may print @ least 2 lines above, , 5 lines below (though more isnt' big deal).
i've looked around, , know piping output grep means search output argument give grep - not sure how other way around?
any appreciated. thanks!
if want use output of command command line argment grep, can use subshell $( ...) syntax.
grep $(grep pattern file.txt | awk 'nr == 1 {line = $0; min = $5} nr > 1 && $5 < min {line = $0; min = $5} end{print $5}') haystack to 2 lines of context above , 5 lines below, can use -a , -b flags.
grep -b2 -a5 $(grep pattern file.txt | awk 'nr == 1 {line = $0; min = $5} nr > 1 && $5 < min {line = $0; min = $5} end{print $5}') haystack
Comments
Post a Comment