如何在Bash中按字母排序顺序获取小数点后的数字

问题描述 投票:0回答:1

我有这个.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.32来到10.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`
linux bash alphabetical
1个回答
0
投票

我希望你的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会给你

2.00
50.1
100

作为奖励,ls -v会给你

2.00  50.1  100
© www.soinside.com 2019 - 2024. All rights reserved.