简体   繁体   English

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

[英]Only keep lines in textfile that start with specific characters

I have large textfile with this structure: 我有这种结构的大文本文件:

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!

For further processing I want to get rid of all lines that do not start with 001, 003 and 005. However, the search criteria must incorporate the position of the 001, 003, 005 at the beginning of the line, as the datasets often contains similar numbers. 为了进行进一步处理,我想摆脱所有不以001、003和005开头的行。但是,搜索条件必须在行的开头并入001、003、005的位置,因为数据集通常包含相似的数字。

So the output I want is this: 所以我想要的输出是这样的:

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!

As I have to do this on various machines a simple Windows OS command would be best (eg like this Delete certain lines in a txt file via a batch file ). 由于我必须在各种计算机上执行此操作,因此最好使用简单的Windows OS命令(例如,像这样,通过批处理文件删除txt文件中的某些行 )。 But I could also life with a python script. 但是我也可以使用python脚本。

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

findstr /b "001 003 005" yourfile.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM