我有一位女士在工作,她给我发电话号码。他们是以一种混乱的方式发送的。每次都是。因此,我想从Skype复制她的全部消息,并让一个批处理文件解析保存的.txt文件,只搜索10个连续的数字。
她派我来的:
Hello more numbers for settings please,
WYK-0123456789
CAMP-0123456789
0123456789
Include 0123456789
This is an urgent number: 0123456789
TIDO: 0123456789
Send to> 0123456789它很乱,唯一的常数是10位数。因此,我希望.bat文件能够对这个怪物进行扫描,并给我留下如下内容:
例如我想要的:
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789我试过下面的this
@echo off
setlocal enableDelayedExpansion
(
for /f %%A in (
'findstr "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" yourFile.txt'
) do (
set "ln=%%A"
echo !ln:~0,9!
)
)>newFile.txt不幸的是,只有当每一行的开头以10位数开始,并且在10位数位于行的中间或末尾的情况下,它才能起作用。
发布于 2017-05-23 12:58:29
不幸的是,一般地解决这个问题是非常困难的。下面的批处理文件正确地从示例文件中获取数字,但是如果您的真实数据包含一个不同格式的数字,程序将失败.当然,在这种情况下,也需要在程序中包含新的格式!:)。
@echo off
setlocal EnableDelayedExpansion
set "digits=0123456789"
(
rem Find lines with 10 consecutive digits (or more)
for /f "delims=" %%A in (
'findstr "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" yourFile.txt'
) do (
set "ln=%%A"
rem Separate line in "words" delimited by space or hypen
set "ln=!ln: =" "!"
set "ln=!ln:-=" "!"
for %%B in ("!ln!") do (
set "word=%%~B"
rem If a word have exactly 10 chars...
if "!word:~9,1!" neq "" if "!word:~10!" equ "" (
rem and the first one is a digit
for /F %%D in ("!word:~0,1!") do (
if "!digits:%%D=!" neq "%digits%" echo !word!
)
)
)
)
) > newFile.txt例如,这个程序将失败,如果一个“单词”与10个字符,即不是一个电话。数字,以数字开头..。
发布于 2017-05-23 17:38:26
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q44134518.txt"
SET "outfile=%destdir%\outfile.txt"
ECHO %time%
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO SET "line=%%a"&CALL :process
)>"%outfile%"
ECHO %time%
GOTO :EOF
:lopchar
SET "line=%line:~1%"
:process
IF "%line:~9,1%"=="" GOTO :eof
SET "candidate=%line:~0,10%"
SET /a count=0
:testlp
SET "char=%candidate:~0,1%"
IF "%char%" gtr "9" GOTO lopchar
IF "%char%" lss "0" GOTO lopchar
SET /a count+=1
IF %count% lss 10 SET "candidate=%candidate:~1%"&GOTO testlp
ECHO %line:~0,10%
GOTO :eof您需要更改sourcedir和destdir的设置,以适应您的情况。我使用了一个名为q44134518.txt的文件,其中包含您的数据以及一些额外的测试。
生成定义为%outfile%的文件
将每一行数据读取到%%a,然后,line。
从line开始处理每个:process。如果没有终止子例程,则查看该行是否为10个或更多字符。
由于该行为10个或更多个字符,请选择前10个字符到candidate,并将count清除为0。
将第一个字符分配给char,并测试‘9’或小于'0'.如果两者都是正确的,请删除line的第一个字符,然后再试一次(直到我们有一个数字或line有9个或更少的字符为止)
计数每一个连续的数字。如果我们还没有计算出10个字符,那么从candidate中删除第一个字符,然后再次检查。
当我们到达10个连续的数字时,echo是line的前10个字符,所有这些字符都是数字和所需的数据。
发布于 2017-05-23 16:40:25
只是另一种选择
@echo off
setlocal enableextensions disabledelayedexpansion
rem Configure
set "file=input.txt"
rem Initializacion
set "counter=0" & set "number="
rem Convert file to a character per line and add ending line
(for /f "delims=" %%a in ('
^( cmd /q /u /c type "%file%" ^& echo( ^)^| find /v ""
') do (
rem See if current character is a number
(for /f "delims=0123456789" %%b in ("%%a") do (
rem Not a number, see if we have retrieved 10 consecutive numbers
set /a "1/((counter+1)%%11)" || (
rem We probably have 10 numbers, check and output data
setlocal enabledelayedexpansion
if !counter!==10 echo !number!
endlocal
)
rem As current character is not a number, initialize
set "counter=0" & set "number="
)) || (
rem Number readed, increase counter and concatenate
set /a "counter+=1"
setlocal enabledelayedexpansion
for %%b in ("!number!") do endlocal & set "number=%%~b%%a"
)
)) 2>nul 其基本思想是使用unicode输出启动一个cmd实例,从该实例中键入文件,并使用find过滤两个字节输出,将每一行输入行扩展为一个字符。
一旦我们将每个字符放在一个单独的行中,并且在一个for /f命令中处理这个输出,那么我们只需要连接一些数字,直到找到一个非数字字符。此时,我们检查是否读取了10组数字,并在需要时输出数据。
https://stackoverflow.com/questions/44134518
复制相似问题