简体   繁体   English

如何使数字以Bash中的字母排序顺序出现在小数点后

[英]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. 我有这个.sh脚本,它遍历父文件夹中的每个文件夹并在每个文件夹中运行program 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. program除其他事项外,获取每个文件夹的名称并将其写入文件data.dat ,以便在此列出所有文件夹名称。 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. program在进入每个文件夹时将文件夹名称写入data.dat ,以便它们以Bash遍历文件夹的顺序显示。

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. 我希望它们按照data.dat的字母顺序排序,无论是1位数字还是2位数字,都将小数字放在大数字之前。 For example, I want 2.32 to come before 10.43 and not the other way around. 例如,我希望2.3210.43之前,而不是相反。

The problem, it seems, is that for Bash the . 看来问题出在Bash身上. 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在Fortran 77中,如下所示:

` program getData `程序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. 我希望您的program将来可能会做得更多,因此我进行了第二个循环。

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. (有些人会因为解析ls的输出而谴责我)等等。

So if you do: 因此,如果您这样做:

mkdir test
cd test
touch 100
touch 2.00
touch 50.1

ls will give you ls会给你的

100  2.00  50.1

ls | sort -n ls | sort -n will give you ls | sort -n会给你

2.00
50.1
100

and, as a bonus, ls -v will give you 作为奖励, ls -v将给您

2.00  50.1  100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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