簡體   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