找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 35405|回复: 7

[文本处理] 批处理怎么删除TXT文本中的指定行?

[复制链接]
发表于 2010-5-30 10:55:05 | 显示全部楼层 |阅读模式
本帖最后由 pcl_test 于 2016-10-15 11:59 编辑

小弟不才~在问个问题

X.TXT中有N行 例如:

1
2
3
4
.....
99999  我想删除前xxx行

怎么写~谢谢!
发表于 2010-5-30 10:58:02 | 显示全部楼层
  1. more +100 1.txt >2.txt
复制代码
 楼主| 发表于 2010-5-30 11:16:35 | 显示全部楼层
原帖由 Batcher 于 2010-5-30 10:58 发表
more +100 1.txt >2.txt


这句代码可以删除前XXX行

可是保留下来的文本不完整 后面的就卡那不动了 可能太大了(4M)
发表于 2010-5-30 11:21:44 | 显示全部楼层
  1. gawk "NR>=100" 1.txt >2.txt
复制代码
发表于 2010-5-30 11:23:02 | 显示全部楼层
上次好像有见到讨论说  文本超过一定长度后命令不能读完。
发表于 2010-5-30 11:23:34 | 显示全部楼层
  1. sed "100,$!d" 1.txt >2.txt
复制代码
发表于 2010-5-30 11:39:08 | 显示全部楼层

标题

for /f "skip=XXX" %%i in ('type 1.txt') do echo %%i>>2.txt
发表于 2010-5-31 00:17:39 | 显示全部楼层
这是我看到的东西,不知道跟主题有关没有,我觉得还是可以看一下。如果看不懂的可以借助GOOGLE。整篇文本复制的,有闲心的不妨看看

可以学习这些代码的defined用法,以及在for里定义变量,for外显示变量来获取指定行数的方法等


  1.     显示指定行的内容

  2. 出处:http://www.cn-dos.net/forum/viewthread.php?tid=21647


  3. 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:-

  4. for /f "delims=" %%a in (input.txt) do ...

  5. for /f "delims=" %%a in ('type input.txt') do ...

  6. for /f "delims=" %%a in ('more ^< input.txt') do ...

  7. 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).

  8. In all the examples, assume the contents of of the file numbers.txt to be:-

  9. one
  10. two
  11. three
  12. four
  13. five
  14. six
  15. seven
  16. eight
  17. nine
  18. ten

  19. Displaying the first line

  20. This example prints one.

  21. @echo off & setlocal ENABLEEXTENSIONS
  22. set "first="
  23. for /f "delims=" %%a in ('more ^< numbers.txt') do (
  24.   if not defined first set first=%%a
  25. )
  26. echo/%first%

  27. Displaying the first X lines

  28. This example prints one, two and three.

  29. @echo off & setlocal ENABLEEXTENSIONS
  30. set "lines=3"
  31. set i=-1
  32. set "ok="
  33. for /f "delims=" %%a in ('more ^< numbers.txt') do (
  34.   set/a i+=1 & for /f %%z in ('echo/%%i%%') do (
  35.     if "%%z"=="%lines%" set ok=1
  36.   )
  37.   if not defined ok echo/%%a
  38. )

  39. Displaying the last line

  40. This example prints ten.

  41. @echo off & setlocal ENABLEEXTENSIONS
  42. for /f "delims=" %%a in ('more ^< numbers.txt') do set "last=%%a"
  43. echo/%last%

  44. Displaying the last X lines

  45. This example prints nine and ten.

  46. @echo off & setlocal ENABLEEXTENSIONS
  47. set "lines=2"
  48. for /f %%a in ('find/c /v "" ^< numbers.txt') do set/a skip=%%a-lines
  49. for /f "delims=" %%a in ('more/e +%skip% ^< numbers.txt') do (
  50.   echo/%%a
  51. )

  52. Displaying the Nth line

  53. 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.

  54. @echo off & setlocal ENABLEEXTENSIONS
  55. set LineNo=3
  56. set "line="
  57. set/a LineNo-=1
  58. for /f "delims=" %%a in ('more/e +%LineNo% ^< numbers.txt') do (
  59.   if not defined line set "line=%%a"
  60. )
  61. echo/%line%

  62. Displaying the Nth line plus X number of lines

  63. This example prints five and six.

  64. @echo off & setlocal ENABLEEXTENSIONS
  65. set start=5
  66. set "lines=2"
  67. set/a i=-1,start-=1
  68. set "ok="
  69. for /f "delims=" %%a in ('more/e +%start% ^< numbers.txt') do (
  70.   set/a i+=1 & for /f %%z in ('echo/%%i%%') do (
  71.     if "%%z"=="%lines%" set ok=1
  72.   )
  73.   if not defined ok echo/%%a
  74. )
复制代码

[ 本帖最后由 sgaizxt001 于 2010-5-31 00:23 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-20 20:52 , Processed in 0.040133 second(s), 17 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表