想象一下,我有一个文件如下文件:
drink eat XXX pizza blunzn sushi我想删除文件中的所有行,从模式XXX之后的第三行开始,因此结果应如下所示:
drink eat XXX pizza blunzn删除XXX后的所有行很简单:
sed -e '/XXX/q' -i data.txt但是,我发现在删除模式后很难跳过固定数量的行。
到目前为止我想出的最好的是:
sed -e '/XXX/ { N; N; q }' -i data.txt有没有比添加n * N更优雅的东西(想象一下,我想跳过50行)?
Imagine I have a file the following file:
drink eat XXX pizza blunzn sushiI would like to remove all lines from the file, starting with the third line after the pattern XXX, so the result should look like:
drink eat XXX pizza blunznRemoving all lines after XXX is simple enough:
sed -e '/XXX/q' -i data.txtHowever, I find it hard to skip a fixed number of lines after the pattern from deletion.
The best I came up with so far is:
sed -e '/XXX/ { N; N; q }' -i data.txtIs there something more elegant, than adding n * N (imagine, I would like to skip 50 lines)??
最满意答案
这可能适合你(GNU sed):
sed '/pattern/{:a;N;s/\n/&/2;Ta;q}' file在遇到所需的模式时,循环所需的行然后退出。
对于遵循所需模式的50行,使用:
sed '/pattern/{:a;N;s/\n/&/50;Ta;q}' fileThis might work for you (GNU sed):
sed '/pattern/{:a;N;s/\n/&/2;Ta;q}' fileOn encountering the required pattern, loop the required lines then quit.
For fifty lines following the required pattern, use:
sed '/pattern/{:a;N;s/\n/&/50;Ta;q}' file更多推荐
发布评论