简体   繁体   中英

Reading files with leading zero bash command line

For example I have 15 files as fallows:

abc01.txt, abc02.txt, ..., ..., abc09.txt, abc10.txt, abc11.txt, ... ... abc15.txt

I want to read these files from command line using bash and perform some operation.

for i in {1..15}; do COMMAND abc$i.txt; done

Above statement only reads files from 10 to 15 because of leading 0 for the first nine files. If I use [0] before $ in the above command then it only reads first 9 files. I want to read all files.

Since bash 4.0 leading zero is supported in {0x..0y} (zero-padded brace expansion). With it, you can do it like this:

for i in {01..15}; do COMMAND "abc$i.txt"; done

You can use printf with zero padded formatting in process substitution :

while read -r f; do
   echo "processing $f"
done < <(printf "abc%02d.txt\n" {1..15})

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