Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
grep -v "\<Swap" instruments.log | awk '{ idx=index($0, "MasterId="); masterId=substr($0, idx+length("MasterId=")+1); masterId=substr(masterId,1,index(masterId,"L")-3); print masterId; }' | xargs grep rel.log

I need to search each of that MasterId/output of awk in rel.log using xargs or something.
How can I do that?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Use while with read to get a line at a time from the pipe, and feed that to grep.

grep -v "\<Swap" instruments.log | \
  awk '{ idx=index($0, "MasterId=");
    masterId=substr($0, idx+length("MasterId=")+1);
    masterId=substr(masterId,1,index(masterId,"L")-3);
    print masterId; }' |\
  while read line; do
    grep -- "$line" rel.log
  done
share|improve this answer
    
Thank you, Your solution works, what if I need to put that in a shell script? grep -v "\<Swap" instruments.log | awk '{ idx=index($0, "MasterId="); masterId=substr($0, idx+length("MasterId=")+1); masterId=substr(masterId,1,index(masterId,"L")-3); print masterId; } ' | while read line; do grep "$line" rel.log done It is giving me syntax error: unexpected end of file –  user14039 Jan 31 '12 at 23:44
    
never mind, got it. Thanks. –  user14039 Jan 31 '12 at 23:50
1  
There's a simpler and faster way: use a multiline pattern. –  Gilles Feb 1 '12 at 0:09
add comment

You can pass a multiline pattern to grep, to search a line that contains a match for any of the patterns. In other words, a multiline pattern is a disjunction of the patterns on each line.

print_one_pattern_per_line | grep -f - rel.log

Incidentally, you could simplify the print_one_pattern_per_line part. Since you're calling awk anyway, you can do the input line matching inside it. And your awk code can be written in a simpler way, using regular expression replacement to remove everything up to MasterId= (assuming that there's a single occurrence of MasterId= on each line, because your code matches the first instance while my regexp below matches the last instance).

<instruments.log awk '
    !/(^|[[:space:]])Swap/ {
        gsub(/.*MasterId=/, "");
        $0 = substr($0, 1, index($0, "L")-3);
        print;
    }' | grep -f - rel.log
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.