简体   繁体   English

遍历子目录并对某些文件执行awk脚本

[英]Iterate through subdirectories and execute an awk script against certain files

I have a directory named ../words that contains a number of subdirectories(words/blabla,words/blabla), in turn each subdirectory contains two types of files (.*1.txt) and (.*2.txt) . 我有一个名为../words的目录,其中包含许多子目录(words / blabla,words / blabla),每个子目录又包含两种类型的文件(。* 1.txt)和(。* 2.txt)。 What I need, it is to execute an AWK script against each one of these files. 我需要的是针对这些文件中的每个文件执行AWK脚本。

Could it be something like? 可能是这样吗?

for d in words/*
do
    for f in .*[0-9].txt
    do
    awk -f script.awk ${f}
    done
done

If you want to keep your for statement structure and apply the awk script to each specified file, you can do the following: 如果要保留for语句结构并将awk脚本应用于每个指定的文件,可以执行以下操作:

for file in $(find words -type f -name ".*[12].txt"); do
    awk -f script.awk "$file"
done

The find command is useful for recursively looking through a directory for any pattern of files. find命令对于递归查找目录中的任何文件模式很有用。


Edit: If your file names contain things like spaces, the above script may not process them properly, so you can do the following instead: 编辑:如果您的文件名包含空格之类的内容,则上述脚本可能无法正确处理它们,因此您可以执行以下操作:

find words -type f -name ".*[12].txt" -print0 | while read -d $'\0' file
do 
    awk -f script.awk "$file"
done

or using xargs: 或使用xargs:

find words -type f -name ".*[12].txt" -print0 | xargs -0 awk -f script.awk

This allows you to delimit your file names with null \\0 characters, so variations in name spacing or other special characters will not be a problem. 这使您可以使用空\\0字符来分隔文件名,因此名称间距或其他特殊字符的变化将不会成为问题。 (You can find more information here: Filenames with spaces breaking for loop, and find command , or here: Handling filenames with spaces , or here: loop through filenames returned by find ). (您可以在此处找到更多信息: 带有空格的文件名中断循环,并找到命令 ;或者:在此处处理带空格的文件名 ;或者在此处: 循环查找find所返回的文件名 )。

鉴于您到目前为止告诉我们的内容,这应该是您所需要的:

awk -f script.awk ../words/blabla/.*[12].txt

如果您需要跳过中间目录级别,仅查看子目录下的内容,则可以使用最大/最小深度

$ find words -maxdepth 2 -mindepth 2 -type f -name '*[0-9].txt' | xargs awk -f ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM