pogibas
pogibas

Reputation: 28339

Redirect command output without overwriting

I do have a command like this:

 grep pattern file > result && head -n 1 file >> result

In which I use grep to extract the specific pattern and head to extract the headline.

Is it possible to simplify my command ? For example to redirect the output from grep?

If I am using this:

 head -n 1 file > result <<(grep pattern file)

I get an error bash: syntax error near unexpected token ('

Upvotes: 0

Views: 2574

Answers (2)

chepner
chepner

Reputation: 531205

You can use a command group to combine the standard output of your two commands:

{ grep pattern file && head -1 file; } > result

Upvotes: 4

Rastislav Hasicek
Rastislav Hasicek

Reputation: 237

If I understand you correctly, you need this:

grep pattern file | head -n 1 file >> result

Send stdout from grep by pipe to head and then append into file result. If you don't want/need it in file result, only on screen, you can use:

grep pattern file | head -n 1 file

I hope I've helped.

Upvotes: -1

Related Questions