Question

I'm trying to treat a bunch of files (five) with an awk command and name the output files using an incrementation. The input files have complicated names. I know how to reuse the files' basenames to rename the outputs but I want to simplify the file names.

this is my code:

for f in *.txt; do
    for i in {1..5}; do
        echo processing file $f
        awk '
            { if ($1=="something" && ($5=="60" || $5=="61"|| $5=="62"|| $5=="63"|| $5=="64"|| $5=="65"|| $5=="66"|| $5=="67"|| $5=="68"|| $5=="69"|| $5=="70"))
            print   }' $b.txt>"file_treated"$i.txt
        echo processing file $f over
    done
done

I understand that the error is in the second line because what I wrote runs the second loop for each value of the first one. I want each value of the first loop to correspond to one value of the second one.

Hope this was clear enough

Was it helpful?

Solution

How about:

i=0
for f in *.txt; do
    let i++;
    awk '$1=="something" && ($5 >= 60 && $5 <=70)' "$f" > file_treated"${i}".txt
done

I simplified your awk command and straightened out your various quoting issues. I also removed the $b.txt since you were simply recreating $f. I left the echo $b etc in case you actually wanted that but it could just as easily be replaced with echo "$f".

OTHER TIPS

Use a counter:

i=1
for f in *.txt
do
    echo "$f is number $((i++))"
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top