user2344435
user2344435

Reputation: 13

Save command output at filename

I've got this problem, where I want to save an output of a command as a filename and stream output from a different command (within the same script) to that file. I wasn't able to find a solution online, so here goes. Below is the code I have:

zgrep --no-filename 'some-patter\|other-pattern' /var/something/something/$1/* | awk -F '\t' '{printf $8; printf "scriptLINEbreakerPARSE"; print $27}' | while read -r line ; do
    awk -F 'scriptLINEbreakerPARSE' '{print $1}' -> save output of this as a filename
    awk -F 'scriptLINEbreakerPARSE' '{print $2}' >> the_filename_from_above
done

So basically I want to use the first awk in the loop to save the output as a filename and then the second awk output will save to the file with that filename.

Any help would be appreciated guys.

Upvotes: 0

Views: 39

Answers (1)

glenn jackman
glenn jackman

Reputation: 247012

You're doing too much work. Just output to the desired file in the first awk command:

zgrep --no-filename 'some-patter\|other-pattern' /var/something/something/$1/* |
  awk -F '\t' '{printf $27 > $8}'

See https://www.gnu.org/software/gawk/manual/html_node/Redirection.html

Upvotes: 1

Related Questions