在sed中使用正则表达式时跳过前n行? [英] Skipping the first n lines when using regex with sed?

查看:343
本文介绍了在sed中使用正则表达式时跳过前n行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sed 中,应用正则表达式时是否可以跳过前n行?我当前正在使用以下内容:

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'

在以下文件上:

Name
John
Albert
Mora
Name
Tommy
Tammy

在一次通过中,我想使用一些正则表达式(其中之一是删除包含 Name 的行,但在这种情况下我想跳过第一行)获得以下信息:

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
Tommy
Tammy

由于文件很大,所以我不想多次通过

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

推荐答案

是的,您可以使用sed命令将<$ c $应用于行范围c> N,M 语法。在这种情况下,您需要这样的东西:

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 }'

这篇关于在sed中使用正则表达式时跳过前n行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆