I am trying to take the results from find and then execute awk to print the first two columns. Then I want to redirect those results to a new file name for each file, using the existing file name with an additional suffix added to the name to. Files in the directory with original source remaining as file.txt and the new files being named file-new.txt

For example:

find -type f -iname "*.txt" -exec awk '{print $1, $2}' {} \;

What do I need to add in order to do this? Would using sed be a better choice?

1 Answer 1


The simplest method is to use awk's output redirection. Awk output redirection is very easy to use in simple cases: the file is opened the first time a redirection is used, and subsequent redirections to the same file name use the existing file descriptor.

If you wanted to add a suffix to the file name, it would be as easy as

find -type f -iname "*.txt" -exec awk '{print $1, $2 >(FILENAME "-new")}' {} +

But you can make a more complex transformation on the file name (here recalculated only at the first line of each input file):

find -type f -iname "*.txt" -exec awk '
    FNR==1 {out=FILENAME; sub(/\.[^.]*$/, "-new&", out) || out = out "-new"}
    {print $1, $2 >out}
' {} +

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .