Board logo

标题: 如何读取文件中的某几行到另外一个文件 [打印本页]

作者: jia332     时间: 2008-5-27 11:02    标题: 如何读取文件中的某几行到另外一个文件
读取文件test1.txt 中的第1,3,4,6,7行到test2.txt test1 内容 port a 1 port b 2 port c 3 port d 4 port e 5 port f 6 port g 7 port h 8 port i 9

作者: huahua0919     时间: 2008-5-27 11:10
@echo off
for /f "delims=" %%i in ('findstr "1 3 4 6 7" a.txt') do (set num=%%i
>>test2.txt call,echo %%num%%
)
pause

作者: jia332     时间: 2008-5-27 11:15
谢谢huahua0919的帮助 如果我test1中的文件内容是这样 SET Port1=0 SET Port2=1 SET Port2=low SET Port1=0 SET Port2=0 SET Port1=0 SET Port2=0 SET Port1=0 SET Port2=0 SET Port3=0 SET Port4=0 SET Port5=1 SET Port5=full SET Port6=1 SET Port6=full SET Port1=0 SET Port2=0 SET Port1=0 SET Port2=0 SET Port1=1 SET Port1=full SET Port2=0 SET Port1=1 SET Port1=full SET Port2=0 SET Port3=0 SET Port4=0 SET Port5=0 SET Port6=0 呢?

作者: HAT     时间: 2008-5-27 11:15
@echo off
setlocal enabledelayedexpansion
set row=0
type nul>"C:\test\test2.txt"
for /f "tokens=1,2* delims=:" %%a in ('findstr /n .* "C:\test\test1.txt"') do (
  set /a row+=1
  if !row! equ 1 (>>"C:\test\test2.txt" echo %%b)
  if !row! equ 3 (>>"C:\test\test2.txt" echo %%b)
  if !row! equ 4 (>>"C:\test\test2.txt" echo %%b)
  if !row! equ 6 (>>"C:\test\test2.txt" echo %%b)
  if !row! equ 7 (>>"C:\test\test2.txt" echo %%b)
)

作者: huahua0919     时间: 2008-5-27 11:18
那就试试HAT兄的代码

作者: bat-zw     时间: 2008-5-27 11:23
Originally posted by HAT at 2008-5-27 11:15: @echo off setlocal enabledelayedexpansion set row=0 type nul>"C:\test\test2.txt" for /f "tokens=1,2* delims=:" %%a in ('findstr /n .* "C:\test\test1.txt"' ...
可简化:
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in (test1.txt) do (
     set str=%%i
     if defined str (
        set /a n+=1
        set _!n!=!str!
     )
)
for %%i in (1 3 4 6 7) do echo !_%%i!>>test2.txt
pause>nul
[ Last edited by zw19750516 on 2008-5-27 at 11:37 AM ]

作者: jia332     时间: 2008-5-27 11:29
谢谢两位高手的回复

作者: jia332     时间: 2008-5-31 10:47
HAT兄 如果是空行为什么显示的是echo is off 而不是空行

作者: HAT     时间: 2008-5-31 10:50
Originally posted by jia332 at 2008-5-31 10:47 AM: HAT兄 如果是空行为什么显示的是echo is off 而不是空行
在计算1,3,4,6,7行的时候,空行是否需要计算在内?

作者: jia332     时间: 2008-5-31 11:17
要计算在内

作者: WANKOILZ     时间: 2008-5-31 12:47
借用zw兄的代码,要计算空行:
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in ('findstr/n ".*" test1.txt') do (
set str=%%i&set str=!str:*:=!
set/a n+=1
set _!n!=!str!
)
for %%i in (1 3 4 6 7) do echo.!_%%i!>>test2.txt
pause

作者: 26933062     时间: 2008-5-31 14:29
@echo off
for %%a in (1 3 4 6 7) do set .%%a=a
for /f "tokens=1* delims=:" %%a in ('findstr/n ".*" test1.txt') do (
  if defined .%%a echo.%%b
)
pause
[ Last edited by 26933062 on 2008-5-31 at 05:11 PM ]

作者: terse     时间: 2008-5-31 17:00
Originally posted by 26933062 at 2008-5-31 14:29: [code] @echo off&setlocal enabledelayedexpansion for %%a in (1 3 4 6 7) do set .%%a=a for /f "tokens=1* delims=:" %%i in ('findstr/n ".*" test1.txt') do ( if defined . ...
楼兄笔误哦 %%i应该%%a 延迟也可以去了吧

作者: 26933062     时间: 2008-5-31 17:12
Originally posted by terse at 2008-5-31 17:00: 楼兄笔误哦 %%i应该%%a 延迟也可以去了吧
粗心,谢提醒,以改正。

作者: jia332     时间: 2008-5-31 18:35
Originally posted by 26933062 at 2008-5-31 14:29:
@echo off
for %%a in (1 3 4 6 7) do set .%%a=a
for /f "tokens=1* delims=:" %%a in ('findstr/n ".*" test1.txt') do (
  if defined .%%a echo.%%b
)
pause
[[i ...
echo. 这个。就可以解决了,兄弟我又学了一招。 十分感谢大家帮助