简体   繁体   English

阅读后如何从文件中删除行?

[英]How to delete lines from file after reading it?

I have a code like this - 我有这样的代码-

     > for line in `cat file.txt`;do 
     /*if line-1 dosomething-1 */
     /*if line-2 dosomething-2 */
     /* if line-3 dosomething-3 */
     //Parsing 3 lines I would like to remove them from file - how to do that?
     //Parse - line4 
     //Parse - line5 
     //Parse - line6
     //now delete above 3 lines again & repeat the process till eof.\
     done

I just need to delete the lines after its processed 我只需要在处理后删除这些行

You can't change a file as you're reading it, so you'll need to output the entries you want to keep to a new file (or stdout), and skip the entries you don't. 您在读取文件时无法更改它,因此您需要将要保留的条目输出到新文件(或stdout)中,并跳过不需要的条目。 Then the resulting file can be processed further with only the kept lines. 然后,仅保留行就可以进一步处理生成的文件。

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

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