Reputation: 23
I am trying to use a script to create several files inside a directory but am struggling with creating separate files for the output from awk.
the awk command is as follows:
$ awk '{ if ($3>=20 && $3<==30 && $5=="Sequenced") print $6 }' ./foo.txt
This produces an example output of:
cat
dog
mouse
What I want to do is to create 3 files from this output that would be say cat.txt, dog.txt, mouse.txt inside a directory ./animals. I would appreciate any help I can get with this.
Upvotes: 0
Views: 363
Reputation: 4004
You have one extra =
in $3<==30
. Fixed below.
awk '$3>=20 && $3<=30 && $5=="Sequenced"{ #No need for if here
file="./animals/"$6".txt" #Build the file name
printf "">file #Creates the empty file
close(file)}' ./foo.txt #Close the stream
One-liner:
awk '$3>=20&&$3<=30&&$5=="Sequenced"{f="./animals/"$6".txt";printf "">f;close(f)}' ./foo.txt
Upvotes: 1
Reputation: 1428
$ awk '{ if ($3>=20 && $3<==30 && $5=="Sequenced") print $6".txt" }' ./foo.txt|xargs touch
using xargs you can transfer your output to argument list to another command, for example touch. It will creates empty files (or updated modification time if file exists)
Upvotes: 1