2012-04-16 70 views
2

我想知道是否有任何可能性按列排序文本文件。例如按文件列按批次排序

aux1.txt像這樣

Name SecondName Grade 

排在外殼我能做到這一點

sort -r -k 3 aux1 

它排序由第3列(級)的文件。

在批處理

sort /+3 < aux1.txt 

排序第三個字母后的文件。

我閱讀了批次的排序手冊,但沒有結果。

回答

5

您可以使用批處理文件編寫自己的整理包裝

您只需將列重新排序到臨時文件中,
對其進行排序並重新排序。 (將近明顯)

REM *** Get the desired colum and place it as first column in the temporary file 
setlocal DisableDelayedExpansion 
(
    for /F "tokens=1-3 delims= " %%A in (aux1.txt) DO (
     set "par1=%%A" 
     set "par2=%%B" 
     set "par3=%%C" 
     setlocal EnableDelayedExpansion 
     echo(!par2! !par1! !par2! !par3! 
     endlocal 
) 
) > aux1.txt.tmp 

REM ** Now sort the first colum, but echo only the rest of the line 
for /F "usebackq tokens=1,* delims= " %%A in (`sort /r aux1.txt.tmp`) DO (
    echo(%%B 
) 
+0

謝謝,它的作品! – vladCovaliov 2012-04-16 13:20:10

0

雖然我真的很喜歡傑布的回答,我一直用克里斯拉羅薩的 TextDB tools多年。

您可以對多個列進行排序,通過他的另一個工具(或任何其他CL工具)進行管道排序。那種舊...

1

對你來說可能已經太晚了,但作爲一般建議,你可以做得比創建一個臨時文件簡單得多。只要管這一切通過排序:

for /f "tokens=1-3" %%a in ('(for /f "tokens=1-3" %%x in (aux1.txt^) do @echo %%z %%x %%y^)^|sort') do echo %%b %%c %%a 

請注意,這是一個簡單的命令,並且可以通過在命令提示符下沒有在所有任何批處理文件打字只是用(必須減少%%的對,當然)。