2012-04-16 69 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 

请注意,这是一个简单的命令,并且可以通过在命令提示符下没有在所有任何批处理文件打字只是用(必须减少%%的对,当然)。