开发者

Skipping the first n lines when using regex with sed?

In sed, is it possible to skip the first n lines when applying a regex? I am currently using the following:

cat test | sed '/^Name/d;/^----------/1;/^(/d;/^$/d'

on the following file:

Name
John
Albert
Mora
Name
Tommy
Tammy

In one pass, I want to use some regexes (one of which is to remove the line containing Name but I want to skip the first line in this case) to obtain the following:

Name
John
Albert
Mora
Tomm开发者_如何学编程y
Tammy

Because the file is huge, I don't want to make multiple passes so any one-pass approaches would be great.


Yes, you can apply sed commands to ranges of lines with the N,M syntax. In this case you want something like this:

sed -e '2,$s/foo/bar/'

An example with delete:

sed -e '2,${ /^Name/d }'
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜