标题: [文本处理] 批处理怎么删除TXT文本中的指定行? [打印本页]
作者: wangl 时间: 2010-5-30 10:55 标题: 批处理怎么删除TXT文本中的指定行?
本帖最后由 pcl_test 于 2016-10-15 11:59 编辑
小弟不才~在问个问题
X.TXT中有N行 例如:
1
2
3
4
.....
99999 我想删除前xxx行
怎么写~谢谢!
作者: Batcher 时间: 2010-5-30 10:58
复制代码
作者: wangl 时间: 2010-5-30 11:16
原帖由 Batcher 于 2010-5-30 10:58 发表
more +100 1.txt >2.txt
这句代码可以删除前XXX行
可是保留下来的文本不完整 后面的就卡那不动了 可能太大了(4M)
作者: CUer 时间: 2010-5-30 11:21
- gawk "NR>=100" 1.txt >2.txt
复制代码
作者: 523066680 时间: 2010-5-30 11:23
上次好像有见到讨论说 文本超过一定长度后命令不能读完。
作者: CUer 时间: 2010-5-30 11:23
- sed "100,$!d" 1.txt >2.txt
复制代码
作者: broly 时间: 2010-5-30 11:39 标题: 标题
for /f "skip=XXX" %%i in ('type 1.txt') do echo %%i>>2.txt
作者: sgaizxt001 时间: 2010-5-31 00:17
这是我看到的东西,不知道跟主题有关没有,我觉得还是可以看一下。如果看不懂的可以借助GOOGLE。整篇文本复制的,有闲心的不妨看看
可以学习这些代码的defined用法,以及在for里定义变量,for外显示变量来获取指定行数的方法等
- 显示指定行的内容
-
- 出处:http://www.cn-dos.net/forum/viewthread.php?tid=21647
-
-
- This page shows how to read specific lines from a text file. There are many ways to have the for /f command read the input file, for instance:-
-
- for /f "delims=" %%a in (input.txt) do ...
-
- for /f "delims=" %%a in ('type input.txt') do ...
-
- for /f "delims=" %%a in ('more ^< input.txt') do ...
-
- However, only the last method (using the more command) will give consistent results across Windows NT, 2000, XP and 2003. The first method does not recognise unicode files. Also, the usebackq switch must be used if the input filename contains spaces. The second method, using the type command, also fails to recognise unicode files on Windows 2000, XP and 2003 if the input file does not begin with a bit order mark (BOM).
-
- In all the examples, assume the contents of of the file numbers.txt to be:-
-
- one
- two
- three
- four
- five
- six
- seven
- eight
- nine
- ten
-
- Displaying the first line
-
- This example prints one.
-
- @echo off & setlocal ENABLEEXTENSIONS
- set "first="
- for /f "delims=" %%a in ('more ^< numbers.txt') do (
- if not defined first set first=%%a
- )
- echo/%first%
-
- Displaying the first X lines
-
- This example prints one, two and three.
-
- @echo off & setlocal ENABLEEXTENSIONS
- set "lines=3"
- set i=-1
- set "ok="
- for /f "delims=" %%a in ('more ^< numbers.txt') do (
- set/a i+=1 & for /f %%z in ('echo/%%i%%') do (
- if "%%z"=="%lines%" set ok=1
- )
- if not defined ok echo/%%a
- )
-
- Displaying the last line
-
- This example prints ten.
-
- @echo off & setlocal ENABLEEXTENSIONS
- for /f "delims=" %%a in ('more ^< numbers.txt') do set "last=%%a"
- echo/%last%
-
- Displaying the last X lines
-
- This example prints nine and ten.
-
- @echo off & setlocal ENABLEEXTENSIONS
- set "lines=2"
- for /f %%a in ('find/c /v "" ^< numbers.txt') do set/a skip=%%a-lines
- for /f "delims=" %%a in ('more/e +%skip% ^< numbers.txt') do (
- echo/%%a
- )
-
- Displaying the Nth line
-
- This example prints three. Note that instead of using the more command's /e switch, the skip option could have been used with the for /f command, however, this fails is it is set to any number less than one.
-
- @echo off & setlocal ENABLEEXTENSIONS
- set LineNo=3
- set "line="
- set/a LineNo-=1
- for /f "delims=" %%a in ('more/e +%LineNo% ^< numbers.txt') do (
- if not defined line set "line=%%a"
- )
- echo/%line%
-
- Displaying the Nth line plus X number of lines
-
- This example prints five and six.
-
- @echo off & setlocal ENABLEEXTENSIONS
- set start=5
- set "lines=2"
- set/a i=-1,start-=1
- set "ok="
- for /f "delims=" %%a in ('more/e +%start% ^< numbers.txt') do (
- set/a i+=1 & for /f %%z in ('echo/%%i%%') do (
- if "%%z"=="%lines%" set ok=1
- )
- if not defined ok echo/%%a
- )
复制代码
[ 本帖最后由 sgaizxt001 于 2010-5-31 00:23 编辑 ]
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |