繁体   English   中英

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

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

我有这个.sh脚本,它遍历父文件夹中的每个文件夹并在每个文件夹中运行program 我使用的代码如下:

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

program除其他事项外,获取每个文件夹的名称并将其写入文件data.dat ,以便在此列出所有文件夹名称。 这些文件夹的名称是标识其内容的数字(十进制)。 program在进入每个文件夹时将文件夹名称写入data.dat ,以便它们以Bash遍历文件夹的顺序显示。

我希望它们按照data.dat的字母顺序排序,无论是1位数字还是2位数字,都将小数字放在大数字之前。 例如,我希望2.3210.43之前,而不是相反。

看来问题出在Bash身上. 按顺序排列在数字之后。 我如何才能将其更改为数字之前?

提前致谢!

编辑: program在Fortran 77中,如下所示:

`程序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`

我希望您的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

有更多的方法可以做到这一点。 例如:

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

(有些人会因为解析ls的输出而谴责我)等等。

因此,如果您这样做:

mkdir test
cd test
touch 100
touch 2.00
touch 50.1

ls会给你的

100  2.00  50.1

ls | sort -n ls | sort -n会给你

2.00
50.1
100

作为奖励, ls -v将给您

2.00  50.1  100

暂无
暂无

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

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