Using awk, how can i redirect the data to all files in the directory?

like ,lets say i want to redirect to file.txt i will do:

echo "abc"|awk '{print >"file.txt"}'

how can i acheive the same thing but to multiple files(in fact all files) in the directory. there are some .txt files in teh cirrent directory. so i want to redirect abc to all the files in the current directory which have an extension of .txt.

有帮助吗?

解决方案

echo abc|awk '{print |"tee files"}'

其他提示

You can use tee:

echo "abc" | tee files/*

If the directory with all these files are in /files/.

One way using GNU awk:

awk -v str="abc" '{ print str > FILENAME; nextfile }' *.txt

This command will overwrite all the content of all files with txt extension with the string abc. You will need the GNU version because of the nextfile instruction, that closes current file and begin to process the next one.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top