简体   繁体   中英

How do you name output files using an increment after a bash file loop?

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

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" .

Use a counter:

i=1
for f in *.txt
do
    echo "$f is number $((i++))"
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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