Board logo

标题: [文本处理] 批处理怎样提取文件中的指定列? [打印本页]

作者: sonorous    时间: 2012-3-19 23:55     标题: 批处理怎样提取文件中的指定列?

原文件内容如下,需要提取一些列赋值到变量:
D:\Temp\1>cat nvrestore.lst
INVISIBLE       File System     backup01 (Saveset 6) 20:42 19 Mar 2012
INVISIBLE       File System     backup02 (Saveset 5) 20:42 19 Mar 2012
INVISIBLE       File System     backup02 (Saveset 4) 20:42 19 Mar 2012
INVISIBLE       File System     backup01 (Saveset 3) 20:42 19 Mar 2012
INVISIBLE       File System     backup02 (Saveset 2) 20:37 19 Mar 2012


要提的内容:INVISIBLE  backup01 6 19Mar2012 ,关健“(Saveset 2)”中的“2”弄不出来,用了delims后,2可以取到,但是其它就乱了

UNIX下可以轻松搞定,请熟悉WIN的TX拔刀相助。
作者: apang    时间: 2012-3-20 09:33

  1. @echo off
  2. for /f "skip=1 tokens=1-10 delims=() " %%a in (test.txt) do (
  3.     echo %%a %%d %%f %%h %%i %%j
  4. )
  5. pause
复制代码

作者: sonorous    时间: 2012-3-20 10:25

apang 发表于 2012-3-20 09:33


有TAB的,不完全是空格,结果如下

D:\Temp\1>for /f "skip=1 tokens=1-10 delims=() " %a in (nvrestore.lst) do (echo %a %d %f %h %i %j)

D:\Temp\1>(echo INVISIBLE       File 5 19 2012   )
INVISIBLE       File 5 19 2012

D:\Temp\1>(echo INVISIBLE       File 4 19 2012   )
INVISIBLE       File 4 19 2012

D:\Temp\1>(echo INVISIBLE       File 3 19 2012   )
INVISIBLE       File 3 19 2012

D:\Temp\1>(echo INVISIBLE       File 2 19 2012   )
INVISIBLE       File 2 19 2012
作者: apang    时间: 2012-3-20 10:44

回复 3# sonorous


在delims=()后面加上tab和空格。
D:\Temp\1>cat nvrestore.lst不是txt文件内容吗?如果不是,删除代码中的skip=1
作者: sonorous    时间: 2012-3-20 11:12

谢了,我用UE转换一下TAB和空格就可以了
作者: find    时间: 2012-3-20 19:05

回复 5# sonorous


没有必要,在delims指定列分隔符的时候多加一个tab符号就行了。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2