繁体   English   中英

循环遍历目录中的每个文件 - bash

[英]Looping through each file in directory - bash

我正在尝试对目录中的每个文件执行某些操作,但是它执行的顺序存在问题。 它应该一次做一个文件。 长行(解压缩、grepping、压缩)在没有脚本的单个文件上运行良好,因此循环存在问题。 有任何想法吗?

脚本应该遍历每个压缩文件并查找 word1 或 word2。 如果至少存在其中之一,则:

  1. 解压文件
  2. grep word1 和 word2 并将其保存到 file_done
  3. 删除解压文件
  4. 使用原始名称将 file_done 压缩到 /donefiles/
  5. 从原始目录中删除 file_done
    #!/bin/bash
    for file in *.gz; do
    counter=$(zgrep -c 'word1\|word2' $file)
    if [[  $counter -gt 0 ]]; then
    echo $counter
    for file in *.gz; do
    filenoext=${file::-3}
    filedone=${filenoext}_done
    echo $file
    echo $filenoext
    echo $filedone
    gunzip  $file | grep 'word1\|word2'  $filenoext > $filedone | rm -f $filenoext |  gzip -f  -c  $filedone > /donefiles/$file | rm -f $filedone
    done
    else
    echo "nothing to do here"
    fi
    done

您提供的代码片段有一些问题,例如循环和错误管道不需要嵌套(整行gunzip $file | grep 'word1\\|word2' $filenoext > $filedone | rm -f $filenoext | gzip... )

另请注意,仅当 *.gz 文件名称中没有空格(或特殊字符)时,您的代码才能正常工作。 此外, zgrep -c 'word1\\|word2'也将匹配line_starts_withword1_orword2_字符串。

这是脚本的工作版本:

#!/bin/bash
for file in *.gz; do
        counter=$(zgrep -c -E 'word1|word2' $file) # now counter is the number of word1/word2 occurences in $file
        if [[ $counter -gt 0 ]]; then
                name=$(basename $file .gz)
                zcat $file | grep -E 'word1|word2' > ${name}_done
                gzip -f -c ${name}_done > /donefiles/$file
                rm -f ${name}_done
        else
                echo 'nothing to do here'
        fi
done

我们在这里可以改进的是:

  • 由于我们无论如何都要解压缩文件以检查 word1|word2 是否存在,因此我们可以对临时文件执行此操作并避免双重解压缩
  • 我们不需要计算文件中有多少 word1 或 word2,我们可以只检查它们的存在
  • ${name}_done 可以是自动清理的临时文件
  • 我们可以使用while循环来处理带空格的文件名
#!/bin/bash
tmp=`mktemp /tmp/gzip_demo.XXXXXX` # create temp file for us
trap "rm -f \"$tmp\"" EXIT INT TERM QUIT HUP # clean $tmp upon exit or termination
find . -maxdepth 1 -mindepth 1 -type f -name '*.gz' | while read f; do
        # quotes around $f are now required in case of spaces in it
        s=$(basename "$f") # short name w/o dir
        gunzip -f -c "$f" | grep -P '\b(word1|word2)\b' > "$tmp"
        [ -s "$tmp" ] && gzip -f -c "$tmp" > "/donefiles/$s" # create archive if anything is found
done

看起来您在外部循环中有一个内部循环:

#!/bin/bash
for file in *.gz; do
    counter=$(zgrep -c 'word1\|word2' $file)
    if [[  $counter -gt 0 ]]; then
        echo $counter
        for file in *.gz; do #<<< HERE
            filenoext=${file::-3}
            filedone=${filenoext}_done
            echo $file
            echo $filenoext
            echo $filedone
            gunzip  $file | grep 'word1\|word2'  $filenoext > $filedone | rm -f $filenoext |  gzip -f  -c  $filedone > /donefiles/$file | rm -f $filedone
        done
    else
        echo "nothing to do here"
    fi
done

如果其中一个文件包含 file1 或 file2,则内部循环遍历目录中的所有文件。 你可能想要这个:

#!/bin/bash
for file in *.gz; do
    counter=$(zgrep -c 'word1\|word2' $file)
    if [[  $counter -gt 0 ]]; then
        echo $counter
        filenoext=${file::-3}
        filedone=${filenoext}_done
        echo $file
        echo $filenoext
        echo $filedone
        gunzip  $file | grep 'word1\|word2'  $filenoext > $filedone | rm -f $filenoext |  gzip -f  -c  $filedone > /donefiles/$file | rm -f $filedone
    else
        echo "nothing to do here"
    fi
done

暂无
暂无

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

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