0

lets say i have few files which have structure like

file1.txt

apple fruit 
tomato vegetable 

file2.txt

apple red green
tomato green yellow

is there a way to grep at once these 2 files (or more) to receive set of data like this one (or similar)

apple fruit red green
tomato vegetable green yellow
2

Use join:

$ join file1.txt file2.txt
apple fruit  red green
tomato vegetable  green yellow
0

Just use grep with the pattern and searched files.
Example:

pi@raspberrypi ~ $ echo "test" > a
pi@raspberrypi ~ $ echo "test" > b
pi@raspberrypi ~ $ echo "test" > c
pi@raspberrypi ~ $ grep "test" a b c 
a:test
b:test
c:test

See the adapted man page: http://www.explainshell.com/explain?cmd=grep+%22pattern%22+a+b+c

See also the full man page: http://www.gnu.org/software/grep/manual/grep.html

  • Your answer would be improved if you would address the multiple file situation in the OP. – Elder Geek Sep 2 '14 at 15:19
  • You're right I think I misread his question. I'm confused now, whether I should remove my answer or not. – Benoit Duffez Sep 2 '14 at 15:44
  • If I were you I'd try to improve it. It might be of interest to view the output of info coreutils 'tee invocation' – Elder Geek Sep 3 '14 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.