2012-05-17 112 views
0

我有一个包含PBX电话长途交换信息的文本文件。 输入文件包含:批处理文件文本处理 - 查找连续号码

sourceNPA,srcNNX,DestinationNPA,destNNX 
954,327,954,201 
954,327,954,202 
954,327,954,203 
954,327,954,210 
954,327,954,212 
954,327,954,213 
954,327,954,214 
etc... 

我不能使用任何但是VBS或Windows批次公司政策原因(不只是因为我不是一个编码器)。我预计会手动完成这些工作,但有43000或更多的转换成范围。

我需要读取给定文本文件的每一行,查看dNPA和dNXX(每行中的最后两个arg)是否连续,如果是这样,请确定范围,以便输入列表在输出中的内容如下所示:

954,327,954,201,203 
954,327,954,210,210 
954,327,954,212,214 
etc... 

我试过研究使用数组,并尝试读取一行到临时文件,但必须有一个技巧。

我一直修修补补,但有一点要说明的是:

@echo off 
setlocal enabledelayedexpansion 
set lineNumber=1 
if exist outputFile.txt del outputFile.txt 

for /f "tokens=1-6 delims=,;" %%a in (inputFile.txt) do call :process %%a %%b %%c %%d 
:EOF 

:process 
set line!linenumber!SrcNPA=%1 
set line!linenumber!SrcNNX=%2 
set line!linenumber!destNPA=%3 
set line!linenumber!destNNX=%4 
REM then intended to compare the values but I'm stuck 
REM I want to compare the last arugment of each line to the same 
REM same argument in the next line read, and if its sequential 
REM then set the start the range and when the comaparison is no longer 
REM consecutive set the top of the range andn then write that range to output 
set /a lineNumber+=1 
+0

我不清楚你想要达到什么目的。 – Marc

+0

我需要处理一个文件,其中包含一列数字,其格式与第一个列出的连续数字相同,并输出格式与第二个数字列表格式相似的数字列表,其中输入中的连续数字转换为输出范围。 – James

+0

代码示例很弱,但这就是为什么我要求提供建议。我尝试了几件事,但我认为我在这里追逐我的尾巴。 – James

回答

2

你需要做的第4号算算看,连续的值。大概有些数字可以从零开始。这会导致批解析问题,因为SET/A假定以0开头的数字是八进制符号。所以需要额外的工作来防止这种情况发生。

假设输入文件预分类,以下应该工作。

@echo off 
setlocal enableDelayedExpansion 
set "current=" 
set "next=" 
(
    for /f "tokens=1-4 delims=," %%A in (testFile.txt) do (
    set /a "num=10000%%D%%10000" 
    if "!current!,!next!"=="%%A,%%B,%%C,!num!" (
     set "end=%%D" 
     set /a "next+=1" 
    ) else (
     if defined current echo !current!,!start!,!end! 
     set "current=%%A,%%B,%%C" 
     set "start=%%D" 
     set "end=%%D" 
     set /a "next=num+1" 
    ) 
) 
    if defined current echo !current!,!start!,!end! 
)>global_new.txt 

如果输入文件未预分类,那么只要每列的宽度恒定,就可以在FOR/F中使用SORT。

for /f "tokens=1-4 delims=," %%A in ('sort testFile.txt') do (

如果列的宽度不恒定并且文件没有预先排序,那么脚本就变得复杂得多。我建议在那个时候切换到VBS。无论如何,VBS会有更好的表现。

+0

完美!它的工作就像你发布的一样。那里有我以前没见过的技术。好东西要学习。辉煌!所有的输入都将按照所描述的格式进行格式化,这样可以用于整个工作......这可以节省我的时间,如果不再更长。感谢您的快速建议。 – James