1

I have several files that I want to process with awk and afterwards pipe the outputs of awk to seperate files. I want to process a single file with awk and write the output to a file, but combining that with find and xargs is problematic for me.
This is the current command:

find ./*.data -print0 | xargs -0 -I{} awk '/^00/ {printf "Data: %s"$0;}' > {}.processed 

This only creates a file and combines all processed data by awk into a single file named

"{}.processed"

.
What is the correct way of writing all output data to separate files?

The filenames of the output for the files "file1.data" and "file2.data" should be:

file1.data.processed
file2.data.processed

asdflair
  • 43
  • 4
  • Possible duplicate of [How to use > in an xargs command?](http://stackoverflow.com/questions/845863/how-to-use-in-an-xargs-command) – Arton Dorneles Mar 04 '16 at 13:37

2 Answers2

1

Why don't you use the FILENAME awk Buildin variable for this:

 awk '/^00/ {printf "Data: %s",$0 >(FILENAME".processed")}' $(find ./*.data)
123
  • 10,778
  • 2
  • 22
  • 45
jijinp
  • 2,592
  • 1
  • 13
  • 15
  • That is perfect. I didn't know that awk can actually output to files. I was always assuming it is only responsible for text processing. Thanks Mr. 7171u – asdflair Mar 04 '16 at 14:21
0

Instead of using xargs you could use a for loop to handle this task:

for F in `find ./*.data`; do
   awk '/^00/ {printf "Data: %s"$0;}' "$F" > "$F".processed
done
Arton Dorneles
  • 1,629
  • 14
  • 18