gpt4 book ai didi

linux - 如何在 Bash 中按字母顺序排列小数点后的数字

转载 作者:太空宇宙 更新时间:2023-11-04 12:00:25 24 4
gpt4 key购买 nike

我有这个 .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,. 按顺序排在数字之后。我怎样才能把它改成在数字之前?

提前致谢!

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

` 程序获取数据

  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`

最佳答案

我预计您的程序将来可能会做更多的事情,因此我做了第二个循环。

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

关于linux - 如何在 Bash 中按字母顺序排列小数点后的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52670112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com