我想知道是否可以按列对文本文件进行排序。例如
我有像这样的行的aux1.txt
Name SecondName Grade在shell中我可以做到这一点。
sort -r -k 3 aux1 它按第三列(等级)对文件进行排序。
成批
sort /+3 < aux1.txt对第三个字母之后的文件进行排序。
我阅读了批处理的排序手册,但没有结果。
发布于 2012-04-16 18:34:27
您可以使用批处理文件编写自己的排序包装器。
您只需要将列重新排序到一个临时文件中,
对它进行排序,然后再重新排序。(近乎显而易见)
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
)发布于 2012-06-08 14:45:33
对你来说可能太晚了,但作为一般的建议,你可以做得比创建一个临时文件简单得多。只需通过管道将其全部通过sort:
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请注意,这是一个单独的命令,只需在命令提示符下输入即可使用,而不需要任何批处理文件(当然,为此必须减少%%)。
发布于 2012-04-17 06:59:56
虽然我真的很喜欢Jeb的回答,但我已经使用Chris LaRosa的TextDB tools很多年了。
您可以对多个列进行排序,通过管道通过他的另一个工具(或任何其他CL工具)。有点老..。
https://stackoverflow.com/questions/10170964
复制相似问题