Splitting Files with awk into other directory

I am trying to split into different files using awk:

cat files | gawk '$1 ~ /---/ || $1 ~ /^deleting$/ || $1 ~ /^sorting$/ || $1 ~ /==/ {print}'| gawk '$1 ~ /---/ || $1 ~ /^deleting$/ || $1 ~ /^sorting$/ || $1 ~ /==/ {print}' |gawk '/[TEST]/{x="F"++i;}{print > x;}'

What I am trying to do is make F* files in the /tmp directory. The command works fine, but the problem is that it only splits the files into F1 F2 etc. into the current directory. I've searched but cannot find a way to put them in a directory other than the present directory, though it should be simple. Anyone have an idea? I would think "/tmp/x" but this didn't work and I've tried variations.

... { print > "/path/to/" X }

You've got two of the exact same awk program in a row, you probably only ned one. And you can simplify that one a whole lot, too, and remove the cat -- awk does not need cat's help to read multiple files.

gawk '$1 ~ /---|(^deleting$)|(^sorting$)|==/' files | gawk '/[TEST]/{ x="F"++i;} {print > "/path/to/" X }'
1 Like

@corona you missed something : small x
my opinion it's good if you close file,you might face problem if many files are opened

/[TEST]/{if(x){close(x)} x="F"++i}
1 Like

Why not

awk '$1 ~ /---|(^(dele|sor)ting$)|==/ {if (/[TEST]/) {if(x) close(x) x="F"++i}; print > "/path/to/" x}' files       
1 Like