简体   繁体   中英

How to get numbers to come after decimal point in alphabetical sorting order in Bash

I have this .sh script that goes through every folder in a parent folder and runs program in each. The code I used was the following:

for d in ./*/
do cp program "$d"
(cd "$d" ; ./program)
done

program , among other things, gets the name of each folder and writes it to a file data.dat , so that all folder names are listed there. These folders' names are numbers (decimal) that identify their contents. program writes the folder name to data.dat when it enters each folder, so that they will appear in the order that Bash goes through the folders.

I want them to be sorted, in data.dat , in alphabetical order, putting lower numbers before higher, regardless of being a 1-digit or 2-digit number. For example, I want 2.32 to come before 10.43 and not the other way around.

The problem, it seems, is that for Bash the . comes after numbers in the order. How can I change it to come before numbers?

Thanks in advance!

EDIT: program is in Fortran 77 and goes like this:

` program getData

  implicit none

  character counter*20, ac*4, bash*270, Xname*4, fname*15  
  double precision Qwallloss, Qrad, Nrad, Qth, QreacSUM
  double precision Xch4in, Ych4in, length, porosity, Uin, RHOin
  double precision MFLR, Area, Xvalue
  integer I

  bash="printf '%s\n'"//' "${PWD##*/}" > RunNumber.txt' 
  call system(bash)                   !this gets the folder name and writes 
                                      !to RunNumber.txt

  open(21, form="FORMATTED", STATUS="OLD", FILE="RunNumber.txt")
  rewind(21)
  read(21,*) counter            !brings the folder name into the program
  close(21)

  `

(...) `

  call system(' cp -rf ../PowerData.dat . ')

  open(27, form="FORMATTED", STATUS="OLD", ACCESS="APPEND", !the new row is appended to the existing file
 1       FILE="PowerData.dat")

  write(27,600) Counter, Xvalue, Nrad, Qrad, Qth,  !writes a row of variables, 
 1     Area, MFLR, Uin, RHOin, Xch4in, Ych4in   !starting with the folder name, 
                                                !to the Data file
  close(27)

  call system('cp -rf PowerData.dat ../')


  end program`

I expect that your program will in the future do perhaps a bit more and therefore I made the second loop.

for d in ./*/ ; do
    echo "$d"a >> /tmp/tmpfile
done
for d in $(sort -n  /tmp/tmpfile) ; do
    cp program "$d"
    (cd "$d" ; ./program)
done

There are more ways to do this; for example:

for d in $(ls | sort -n) ; do

(some will castigate me for parsing the output of ls ) etcetera.

So if you do:

mkdir test
cd test
touch 100
touch 2.00
touch 50.1

ls will give you

100  2.00  50.1

ls | sort -n ls | sort -n will give you

2.00
50.1
100

and, as a bonus, ls -v will give you

2.00  50.1  100

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