仅保留文本文件中以特定字符开头的行

时间:2015-10-08 09:35:14

标签: python string batch-file

我有这种结构的大文本文件:

MTXT file Version 1
    file    001 good stuff
    file    002 sdfdsfsf
    file    003 cool stuff
    file    004 fjgfhjhgj
base64
    file    005 more cool stuff
    file    006 dgfdgfdgfcf
    file    007 dfgdgffdg
   -
009 fsf002dsdfds
010 dsfsfd003dsfs
011 sdf005sd001fs
001 IMPORTANT STUFF with numbers than can also contain 001, 002, 005 etc!
002 asfdasdsa
003 IMPORTANT STUFF with numbers than can also contain 001, 002, 005 etc!
004 vld2004sfsfd005sfds
005 IMPORTANT STUFF with numbers than can also contain 001, 002, 005 etc!

为了进一步处理,我想摆脱所有不以001,003和005开头的行。但是,搜索条件必须包含001,003,005在行首的位置,作为数据集通常包含相似的数字。

所以我想要的输出是:

001 IMPORTANT STUFF with numbers than can also contain 001, 002, 005 etc!
003 IMPORTANT STUFF with numbers than can also contain 001, 002, 005 etc!
005 IMPORTANT STUFF with numbers than can also contain 001, 002, 005 etc!

由于我必须在各种机器上执行此操作,因此最简单的Windows操作系统命令(例如,像Delete certain lines in a txt file via a batch file)。 但我也可以使用python脚本生活。

1 个答案:

答案 0 :(得分:3)

使用findstr /b在一行的开头查找任何指定的以空格分隔的数字:

findstr /b "001 003 005" yourfile.txt