To find a string in a file and print first column of the output, we can use
grep "foo" file.txt | awk '{print $1}'
which can be done using awk
alone
awk '/foo/ {print $1}' file.txt
(https://stackoverflow.com/a/22866418/1662898).
Instead of a single string (foo) as a pattern, I want to search for a list of strings in a file. Using grep
, it would be
grep -f file.txt file2.txt | awk '{print $1}' > outFile.txt
Can I do the same using awk
alone?
file.txt
abcd
acde
a2rt
file2.txt
1 albcd dhakd kdf
3 abcdbd and
2a bda2rt tert
outFile.txt
3
2a
Thanks! Abhishek