开发者

Delete file contents after line == "xxxxxxx"

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜