Shell – Is there any faster way to get this output file in linux

grepshell-scripttext processing

cat file_1

my colour is red
my rose is red
my colour is blue
my rose id blue

cat file_2 
red
blue

cat output_file should be
my colour is red
my colour is blue

here i am using

cat file_2 | while read line;do cat file_1 | grep "$line" | head -1;done

here i am trying to get the top most line containing the pattern "red" and "blue" which is present in the file_2

is there any other way to do , as fast as possible, while loop is taking time

Best Answer

You can use a while construct to loop over the patterns from file2 and then use -m 1 with grep to stop after first match on file1:

while IFS= read -r i; do grep -Fm1 "$i" file1; done <file2
  • -F treats the pattern literally
  • -m 1 makes grep to exit after first match

Shell loops are usually not efficient, but given the pattern list is small it is usable in this case.

Faster alternative, xargs:

xargs -a file2 -n1 -P2 -I'{}' grep -Fm1 {} file1

Use more parallel processes (-P) for more patterns.

Example:

% while IFS= read -r i; do grep -Fm1 "$i" file1; done <file2
my colour is red
my colour is blue

% xargs -a file2 -n1 -P2 -I'{}' grep -Fm1 {} file1
my colour is blue
my colour is red
Related Question