使用cmd从文本文件打印特定行

时间:2017-04-28 18:10:35

标签: batch-file for-loop cmd

我正在编写一个搜索特定帐号的脚本,如果线条原样,应翻转其标志并打印其余部分。
文件中的数据:
ORG1 11月,300,771717.684,874790.678,874790.678,874790.678 ORG2 11月,304,-647647.789,-647647.789,-647647.789,-647647.789 Org3 11月,670,-67890.908,-67890.908,-67890.908,-67890.908 Org4 11月,671,89765.908,89765.908,89765.908,89765.908
...............
.............
..............
和其余的行
我的脚本如下:

chmod +x /home/
chmod +x /home/username
chmod +x /home/username/siteroot

所以输出应该是:

 @echo off
 setlocal EnableDelayedExpansion 

SET "source=Test.txt"
SEt "target=test001.txt"
set "search2=670"
set "search1=669"

rem searches for "669" and finds the number of lines till that
for /F "delims=:" %%a in ('findstr /N "^.*,.*,%search1%" %source%') do set /A lines=%%a-1

rem Reading from the source file
< %source% (


rem Print the number of lines before the search1

rem Copy the lines previous to target one
for /L %%i in (1,1,%lines%) do set /P "line=" & echo !line!


  rem Read and process the search1 line
  set /P "line="
  for /F "tokens=1-7 delims=," %%a in ("!line!") do (
  set "data1=-%%d" & set "data2=-%%e" & set "data3=-%%f" & set "data4=-%%g"
  echo %%a,%%b,%%c,!data1:--=!,!data2:--=!,!data3:--=!,!data4:--=!)

Rem here the script finds  670 and prints that line, this works perfect
for /F "tokens=1-7 delims=," %%a in ('findstr /R "^.*,.*,%search2%" 
%source%') do (
set "data1=-%%d" & set "data2=-%%e" & set "data3=-%%f" & set "data4=-%%g"
echo %%a,%%b,%%c,!data1:--=!,!data2:--=!,!data3:--=!,!data4:--=! )>> 
%target%

Rem her the script should pick the lines after the row that has 670 in it, which could be in any lines in the script 

for /F "delims=:" %%b in ('findstr /N "^" %source%') do set /A linea=%%b


for /F "delims=:" %%a in ('findstr /N "^.*,.*,%search2%" %source%') do set /A linez=%%a+1

Rem print lines starting after the row that has 670 till the end of file
< %source% (
for /L %%i in (%linez%,1,%linea%) do set /P "line=" & echo !line! )>> 
%target%

请指导我。 感谢

2 个答案:

答案 0 :(得分:0)

  • 第一个findstr将对行进行编号,
  • 第3列中的search2的第2个过滤器
  • for /f会拆分行号
@echo off&setlocal
Set "source=Test.txt"
Set "target=test001.txt"
Set "search2=670"
Set "LineZ="
For /F "tokens=1* delims=:" %%A in (
  'findstr /n "^" "%source%" ^|findstr "^[0-9]*:.*,.*,%search2%" '
) Do If not defined LineZ Set "LineZ=%%A"&Set "Line=%%B"
(Echo:%Line:-=%
 More +%LineZ% "%source%"
)>"%target%"

我不清楚-的字符串替换是什么。

修改包括删除减号。

答案 1 :(得分:0)

改变/扩展正在进行的问题是一个错误的决定,因为:

  • 跟进读者有问题要了解最新情况,而不阅读问题的编辑和所有评论。
  • 你将观众限制在你的问题上,而这个问题主要集中在未答复/未经检查的问题上。

所以,当你得到满意的答案时,提出一个新问题

批量浮动数字的最后一次请求不是那么容易,因为唯一的变量类型是字符串(如果可能,转换为Set /AIf语句中的整数)。
我想以下批次过于复杂:

  • 将该行传递给子
  • 将字段存储到数组Arg %% I
  • 迭代Arg 4..7在.分割,并将整数部分与-1相乘
  • 将结果存回数组
  • 重新组合所有数组元素的行。

Pascal,Goethe,Twain的引用类似,我没有时间写一个较短的批次; - )

@Echo off&SetLocal EnableDelayedExpansion
Set "source=Test.txt"
Set "target=test001.txt"
Set "search2=670"
Set "LineZ="
For /F "tokens=1* delims=:" %%A in (
  'findstr /n "^" "%source%" ^|findstr "^[0-9]*:.*,.*,%search2%" '
) Do If not defined LineZ Set /A "LineZ=%%A,Cnt=0"&Call :Sub %%B
(Echo:%Line:-=%
More +%LineZ% "%source%"
)>"%target%"
Goto :Eof

:Sub
If "%~1" Neq "" Set /A Cnt+=1&Set "Arg!Cnt!=%1"&Shift&Goto :Sub
For /l %%I in (4,1,7) DO For /f "tokens=1* delims=." %%N in ("!Arg%%I!"
  ) DO Set /A "Arg%%I=-1*%%N"&Set "Arg%%I=!Arg%%I!.%%O"
Set "Line="
For /F "tokens=2 delims==" %%L in ('Set Arg') Do Set "Line=!Line!,%%L"
Set "Line=%Line:~1%"