sed 脚本:如何在 n 之后搜索.线? [英] sed scripting : how to search after n no. of lines?

查看:36
本文介绍了sed 脚本:如何在 n 之后搜索.线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在大文件中的 'n' 行之后使用 grep 捕获模式的第一次出现?

How to capture the first occurrence of a pattern using grep after 'n' numbers of lines in a large size file ?

例如,我有 1000 行代码,其中 'wire' 在第 451 行之前和之后出现.我想在第 451 行之后抓取第一次出现的电线.

For instance, I have 1000 lines of code in which 'wire' occur before and after 451st line. I want to grab the first occurrence of wire after 451st line .

推荐答案

您可以使用 sed 的范围表达式轻松执行此任务.例如:

You can use sed's range expressions to perform this task easily. For example:

sed -n '452,$ { /wire/ {p;q} }' /tmp/foo

这将跳过前 451 行,然后扫描每一行直到 EOF 为wire".找到后,将打印模式空间然后退出.

This will skip the first 451 lines, then scan each line until EOF for "wire." When found, it will print the pattern space and then quit.

这篇关于sed 脚本:如何在 n 之后搜索.线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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