Bash Script changing the output file for every file

给你一囗甜甜゛ 提交于 2021-02-11 12:39:15

问题


I'm trying to use a command on everyfile .log in a folder but I can't understand how to change the output file for every file in the folder.

#!/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

How can i increment the counter for log$n.txt?


回答1:


It's bad practice to write shell loops just to process text (see https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice). This is all you need:

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

You need to increment the variable 'N' over every iteration and remove the double-quotes in the for loop



来源:https://stackoverflow.com/questions/35936374/bash-script-changing-the-output-file-for-every-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!