I am working on Unix command line. I have two files. I want to cat file1.txt and grep the output in file2.txtFile1.txt123A223A143A153A183A123J123PFile2.txt:123A (TYU)223A (RUT)143A (EWRW) 4153A (TGBW) 89 ()183A (23) YHYT 123J ikik 780123P haja 123XRQE haja 123XRQE haja 909The last 2 lines of file2.txt are not present in file1.txtI am trying to do something like:head file1.txt | **and ??** grep file2.txtI tried with xargs, $variable to store it, but shell hangs or doesn't work.Expected Output: Edited:123A (TYU)223A (RUT)143A (EWRW) 4153A (TGBW) 89 ()183A (23) YHYT 123J ikik 780123P haja 123
Store output from one command and process it for another
bash;text processing;grep
I'm assuming you want to search for strings that are present in file2.txt using the strings from file1.txt?If so you can use grep's -f switch to accomplish this.excerpt from grep's man page  -f FILE, --file=FILEObtain patterns from FILE, one per line.  The empty file contains zero   patterns, and therefore matches nothing.  (-f is specified by POSIX.)So try the following:$ grep -f file1.txt file2.txt 123A (TYU)223A (RUT)143A (EWRW) 4153A (TGBW) 89 ()183A (23) YHYT 123J ikik 780123P haja 123Passing a portion of file1.txtIf on the other hand you'd like to only search for a portion of the strings present in file1.txt you could use process substitution to dynamically generate a subset of file1.txt and pass that as a temporary file to grep's -f switch.For example:$ grep -f <(head -5 file1.txt) file2.txt 123A (TYU)223A (RUT)143A (EWRW) 4153A (TGBW) 89 ()183A (23) YHYT This will take the first 5 lines from file1.txt and pass them in as a temprary file via the <(..) notation to grep.