【问题标题】:Bash Script changing the output file for every fileBash 脚本更改每个文件的输出文件
【发布时间】:2016-03-11 09:24:22
【问题描述】:

我正在尝试对文件夹中的每个文件 .log 使用命令,但我不明白如何更改文件夹中每个文件的输出文件。

#!/bin/bash
N="0"
for i in "*.log"
do
    echo "Processing $f file..."
    cat $i | grep "test" | awk '/show/ {a = $1} !/show/{print a,$6}' > "log$N.txt"
done

如何增加 log$n.txt 的计数器?

【问题讨论】:

  • ((N++)) 或者我不知道在 bash 中查找算术可能是一个好的开始。

标签: bash file awk output


【解决方案1】:

编写 shell 循环只是为了处理文本是不好的做法(请参阅https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice)。这就是你所需要的:

awk '
    FNR==1  { print "Processing", FILENAME; a=""; close(out); out="log" fileNr++ ".txt" }
    !/test/ { next }
    /show/  { a = $1; next }
    { print a, $6 > out }
' *.log

【讨论】:

    【解决方案2】:
    #!/bin/bash
    N="0"
    for i in *.log
    do
        echo "Processing $f file..."
        cat $i | grep "test" | awk '/show/ {a = $1} !/show/{print a,$6}' > "log$N.txt"
        N=$((N+1))
    done
    

    您需要在每次迭代中递增变量 'N' 并删除 for 循环中的双引号

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      相关资源
      最近更新 更多