开发者

Delete file contents after line == "xxxxxxx"

开发者 https://www.devze.com 2023-02-01 13:14 出处:网络
I have a bunch of files I need to clean. I need to delete all lines below a line that is equal to something (that line should be removed too).

I have a bunch of files I need to clean. I need to delete all lines below a line that is equal to something (that line should be removed too).

How can I do that in bash?

I 开发者_JS百科just need the example of how to delete the lines, I can loop all the files myself.


Easiest to just tell sed to quit when it sees it.

sed -n '/xxxxxxx/q;p' input.txt


You can use Perl:

perl -i.bak -pe 'exit if /xxxxxxx/' filename.txt ...

This will replace files in place.


'sed' is the easiest way to do this but if you have a bit more complex requirement You can also use 'awk'.

For this particular case, create a file 'foo.awk' with following content:

{
    if ($0 == "xxxxxx")
            exit
    else
            print $0
}

and from shell invoke the following command:

awk -f foo.awk $FILE_NAME
0

精彩评论

暂无评论...
验证码 换一张
取 消