0

I have these excel files in a user directory:

430_XFRtoDML.xlsx
431_XFRtoDML.xlsx
440_XFRtoDML.xlsx
450_XFRtoDML.xlsx
451_XFRtoDML.xlsx
465_XFRtoDML.xlsx
500_XFRtoDML.xlsx

Path: /home/user

I want to write a script to create a text file bit_list.txt mentined below. It should remove the xlsx extension followed by :5. and first 3 number from that file. e.g. 430_XFRtoDML:5. 430

filenames=`ls *.xlsx`
for eachfile in $filenames
do
   echo $eachfile
done

Expected output:

$ cat bit_list.txt
430_XFRtoDML:5. 430
431_XFRtoDML:5. 431
440_XFRtoDML:5. 440
450_XFRtoDML:5. 450
451_XFRtoDML:5. 451
465_XFRtoDML:5. 465
500_XFRtoDML:5. 500
pLumo
  • 22,565

1 Answers1

2
  1. Don't parse ls, rather use *.xlsx directly in the for-loop.
  2. Make use of the shells parameter substitution possibilities.
  3. Use printf instead of echo.
for file in *.xlsx; do
    name="${file%.*}"
    num="${file:0:3}"
    printf '%s:5. %d\n' "$name" "$num"
done > bit_list.txt
pLumo
  • 22,565