我在stackoverflow中阅读了很多文章,但我仍然无法进行排序。
这种情况是这样的:
我有一个9列没有标题的csv文件,如下所示:
D,0000001,2016/01/01 01:00,111,0.000,0000008,10.000,NET,Computer
B,0000002,2016/01/01 01:30,111,0.000,0000001,10.000,NET,Computer
C,0000003,2016/01/01 02:00,121,0.000,0000001,10.000,ORG,Computer
E,0000001,2016/01/01 02:00,121,0.000,0000003,10.000,ORG,Computer
E,0000001,2016/01/01 02:00,121,0.000,0000003,10.000,COM,Computer
C,0000001,2016/01/01 02:00,121,0.000,0000002,10.000,COM,Computer
我想要的输出是2(000001),6(0000008),8(NET)列的顺序,并输出到新的csv文件,如下所示:
C,0000001,2016/01/01 02:00,121,0.000,0000002,10.000,COM,Computer
E,0000001,2016/01/01 02:00,121,0.000,0000003,10.000,COM,Computer
E,0000001,2016/01/01 02:00,121,0.000,0000003,10.000,ORG,Computer
D,0000001,2016/01/01 01:00,111,0.000,0000008,10.000,NET,Computer
B,0000002,2016/01/01 01:30,111,0.000,0000001,10.000,NET,Computer
C,0000003,2016/01/01 02:00,121,0.000,0000001,10.000,ORG,Computer
我尝试了一些这样的代码但不起作用:
@echo off
setlocal
for /F "tokens=1-9 delims=," %%a in (input.csv) do set a[%%b,%%a,%%c,%%d,%%e,%%f,%%g,%%h,%%i] =%%
for /F "tokens=2-10 delims=[,]=" %%a in ('set a[') do echo %%b,%%a,%%c,%%d,%%e,%%f,%%g,%%h,%%i
有人可以帮助我吗?我是键入命令脚本的新手。
答案 0 :(得分:3)
你的方法非常好。只是字段值的顺序设置不正确,无法获得输出所需的排序顺序。
@echo off
if not exist input.csv goto :EOF
setlocal
for /F "tokens=1-9 delims=," %%a in (input.csv) do set "a[%%b,%%f,%%h,%%a,%%c,%%d,%%e,%%g,%%i]=%%"
del input.csv
for /F "tokens=2-10 delims=[,]=" %%a in ('set a[') do echo %%d,%%a,%%e,%%f,%%g,%%b,%%h,%%c,%%i>>input.csv
endlocal
此处使用%%b,%%f,%%h,%%a,%%c,%%d,%%e,%%g,%%i
而不是%%b,%%a,%%c,%%d,%%e,%%f,%%g,%%h,%%i
创建环境变量,当然在输出时也需要%%d,%%a,%%e,%%f,%%g,%%b,%%h,%%c,%%i
而不是%%b,%%a,%%c,%%d,%%e,%%f,%%g,%%h,%%i
。
答案 1 :(得分:2)
你几乎拥有它:
@echo off
setlocal
for /F "tokens=1-9 delims=," %%a in (input.csv) do set "a[%%b%%f%%h]=%%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i"
for /F "tokens=2 delims==" %%a in ('set a[') do echo %%a
但是,您显示的结果是 not ,如您在问题中所述。如果您想要%%f
字段下的逆序,您可以使用:
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1-9 delims=," %%a in (input.csv) do (
set /A "invF=100000000-1%%f"
set "a[%%b!invF!%%h]=%%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i"
)
for /F "tokens=2 delims==" %%a in ('set a[') do echo %%a