标题: 请教搜索提取特定位置的字符难题
[打印本页]
作者: cain
时间: 2006-8-27 15:54
标题: 请教搜索提取特定位置的字符难题
在NT CMD下,要提取一TXT文件中第N行(如第5行)中的从40列开始的6个字符并使之成为一变量。要求不要用第三方软件,请达者告知。
作者: electronixtar
时间: 2006-8-27 16:06
As far as I know, try this command:
more +40 C:\somefile.txt
This can print the 40th line to screen, and use Cmd variables to cut string from the the 6th, use 'for' to set the string as a final variable.
作者: cain
时间: 2006-8-27 16:34
Quote: |
Originally posted by electronixtar at 2006-8-27 16:06:
As far as I know, try this command:
more +40 C:\somefile.txt This can print the 40th line to screen, and use Cmd variables to cut string from the the 6th, use 'for' to se ... |
|
谢谢你。不过你这个不对的,据more /?了解,“+n 从第 n 行开始显示第一个文件”
作者: NaturalJ0
时间: 2006-8-27 17:38
for 有个 skip 参数,可以试下。
作者: 3742668
时间: 2006-8-27 19:12
关于获得文件指定行的内容,个人认为最简单莫过于使用
findstr了:
:dosomething
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=: tokens=1,2" %%i in ('findstr /n . %1 ^| findstr "^%2" ') do set "str=%%j"
if "!str:~%3,1!" == "" (echo 该行没有第%3个字符... && goto :eof)
echo !str:~%3,1!
endlocal
goto :eof
拷贝上面的代码,粘贴到你的脚本末尾,如果标号dosomething前面非空白最后一行内容不是goto :eof则自行加上。
可以在你的批处理中使用
call :dosomething 参数1 参数2 参数3来调用。
注:
参数1:要查看的文件完整路径以及文件名
参数2:行号
参数3:字符的位置
作者: zh159
时间: 2006-8-28 03:06
楼上的有点错误:
设%3=10,“echo !str:~10,1!”显示的字符不是第“10”个字符,而是“10+1”个字符,因为第一位字符是“echo !str:~0,1!”,所以要把“%3”转换一下:“%3-1”
用“findstr /n . %1 ^| findstr "^%2"”方式要先给所有行加上行号再搜索设定的行数
改写了一个:
Quote: |
:dosomething
set/a X=%3-1
set N=0
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%i in (%1) do (set/a N=!N!+1
if "!N!"=="%2" set "str=%%i"&goto str)
:str
if "!str:~%X%,1!"=="" echo 该行没有第%3个字符&goto :eof
if "!str:~%X%,1!"==" " echo 该行第%3个字符是空格&goto :eof
echo !str:~%X%,1!
endlocal
goto :eof |
|
(显示空格一行可以不要)
用“set/a N=!N!+1”计数,当达到“if "!N!"=="%2"”时“set "str=%%i"&goto str”
这样不用搜索全部的行
作者: cain
时间: 2006-8-28 08:46
经测试,5楼版主的代码不能正确获取;6楼代码同样出错,代码中没有使用到findstr。
作者: NaturalJ0
时间: 2006-8-28 09:44
for /f "skip=
4 tokens=*" %%i in (
file.txt) do (
set
str=%%i
goto OUTFOR)
:OUTFOR
set
str=%
str:~
39,
6%
[
Last edited by NaturalJ0 on 2006-8-28 at 09:54 ]
作者: cain
时间: 2006-8-28 11:55
Quote: |
Originally posted by NaturalJ0 at 2006-8-28 09:44:
for /f "skip=4 tokens=*" %%i in (file.txt) do (
set str=%%i
goto OUTFOR)
:OUTFOR
set str=%str[/c ... |
|
强!测试通过!太感谢了!再请问一下高手:如果要查找该文本文件中所有行的第40列开始的6个字符,有没有简化的方法?
作者: NaturalJ0
时间: 2006-8-28 13:10
setlocal EnableDelayedExpansion
for /f "tokens=*" %%i in (
file.txt) do (
set str=%%i
set str=!str:~
39,
6!
>>
newfile.txt echo !str!)
endlocal
newfile.txt 中是你要的内容,每行对应原来每行中第40列起的6个字符。
[
Last edited by NaturalJ0 on 2006-8-28 at 13:14 ]
作者: cain
时间: 2006-8-28 15:12
NaturalJ0实在高。现仍有二个问题:
一是在8楼代码中,我想同时实现查找第30列开始的3个字符,改成如下,但不成功:
for /f "skip=4 tokens=*" %%i in (file.txt) do (
set str=%%i
goto OUTFOR)
:OUTFOR
set str=%str:~39,6%
set str2=%str:~29,3%
二是在10代码中,我想实现的是查找该文本文件中所有行的第40列开始的6个字符并把每行中的结果独立成一个变量,不知能否?
作者: NaturalJ0
时间: 2006-8-28 15:41
高什么高,偶也才来这里混了几天,大多是现学现用。上面几行代码还是前几天跟别人学的呢。
for /f "skip=4 tokens=*" %%i in (file.txt) do (
set str=%%i
goto OUTFOR)
:OUTFOR
set str
1=%str:~39,6%
set str2=%str:~29,3%
如果全要设成变量这样试试看
setlocal EnableDelayedExpansion
set/a num=0
for /f "tokens=*" %%i in (
file.txt) do (
set str=%%i
set str=!str:~
39,
6!
set/a num=!num!+1
set str!num!=!str!)
set str //这条语句是为了显示下运行结果,看看变量是否符合要求
endlocal //这些变量是临时的,只能在 endlocal 前使用,endlocal 语句执行后就自动消亡了。
[
Last edited by NaturalJ0 on 2006-8-28 at 15:45 ]
作者: cain
时间: 2006-8-28 16:18
for /f "skip=4 tokens=*" %%i in (file.txt) do (
set str=%%i
goto OUTFOR)
:OUTFOR
set str1=%str:~39,6%
set str2=%str:~29,3%
这样还是不行的,我也觉得奇怪,str2就是没有任何内容。
作者: NaturalJ0
时间: 2006-8-28 17:32
我试有内容啊
作者: cain
时间: 2006-8-28 17:36
Quote: |
Originally posted by NaturalJ0 at 2006-8-28 17:32:
我试有内容啊 |
|
不好意思,是我搞错了。不过下面这个的确通不过:
setlocal EnableDelayedExpansion
set/a num=0
for /f "tokens=*" %%i in (file.txt) do (
set str=%%i
set str=!str:~39,6!
set/a num=!num!+1
set str!num!=!str!)
set str //这条语句是为了显示下运行结果,看看变量是否符合要求
endlocal //这些变量是临时的,只能在 endlocal 前使用,endlocal 语句执行后就自动消亡了。
作者: NaturalJ0
时间: 2006-8-28 18:31
错哪儿了?
作者: zh159
时间: 2006-8-28 22:36
Quote: |
Originally posted by cain at 2006-8-28 08:46:
经测试,5楼版主的代码不能正确获取;6楼代码同样出错,代码中没有使用到findstr。 |
|
注意使用方法:
Quote: |
Originally posted by 3742668 at 2006-8-27 19:12:
拷贝上面的代码,粘贴到你的脚本末尾,如果标号dosomething前面非空白最后一行内容不是goto :eof则自行加上。
可以在你的批处理中使用 call :dosomething 参数1 参数2 参数3来调用。
注:
参数1:要查看的文件完整路径以及文件名
参数2:行号
参数3:字符的位置 |
|
NaturalJ0的“skip=4”应该是最快的
作者: cain
时间: 2006-8-28 22:50
Quote: |
Originally posted by NaturalJ0 at 2006-8-28 18:31:
错哪儿了? |
|
最后str只是一组三位的数字而已,没有全部。
作者: zh159
时间: 2006-8-28 22:56
LZ应该写正确:第N行的第N个字符开始后面N个字符
“第N个字符开始”写为“第N列开始”容易理解错误只要第N个字符
[
Last edited by zxcv on 2006-8-28 at 22:57 ]
作者: NaturalJ0
时间: 2006-8-28 23:27
Quote: |
Originally posted by cain at 2006-8-28 22:50:
最后str只是一组三位的数字而已,没有全部。 |
|
我没明白你这句话的意思。你要的变量是 str1 str2 str3 ...... 你运行后输出结果是什么?