Ancalima
Ancalima

Reputation: 1

Bash Script changing the output file for every file

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?

Upvotes: 0

Views: 106

Answers (2)

Ed Morton
Ed Morton

Reputation: 204259

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

Upvotes: 3

Inian
Inian

Reputation: 85800

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

Upvotes: 1

Related Questions