-1

Trying to get the output of a grep written into a file while having the by grep non-affected output in terminal.

Example:

command:

cat file | grep 'aaa' >> any.txt #Missing parameters here

desired output in terminal:

aaa
bbb
ccc

redirected into any.txt

aaa
Ja Bush
  • 13
  • 4

1 Answers1

0

Then maybe not grep, you can use awk instead:

cat file | awk '/aaa/{print >>"output.txt";}1'
Til
  • 5,150
  • 13
  • 26
  • 34
  • That's a nice solution, can I somehow have that affect the entire line while not knowing what's inside the whole document? – Ja Bush Jan 30 '19 at 14:19
  • f.e. knowing that the line i want somewhere includes aaa but it's not the entire line – Ja Bush Jan 30 '19 at 14:20
  • @JaBush Pardon, I don't understand you. – Til Jan 30 '19 at 14:21
  • Nevermind, your comment already exactly solved what i needed. I expected it to work like "grep -o" which only greps the given text, but what you wrote grabs the entire line which includes the searched term. – Ja Bush Jan 30 '19 at 14:27
  • @JaBush If you want it to work like `grep -o`, you can use `gsub` or `match` first, and then `print`. Try to find and read documents of `awk`, you'll find it powerful :) – Til Jan 30 '19 at 14:30