首页 > 解决方案 > sed 找到带有集合文章的行 (a, an, the)

问题描述

我应该找到我自己设置的文章的行。在前 10 行中搜索。

sed -n '/the/p' file.txt | sed -n '/ a /p' file.txt 

将仅显示最后一个命令的输出。还

sed -n '1,10 /the/p' file

或者

sed -n 1,10p '/the/p' file

不工作,我发现范围的使用只与像这样的模式

[range]/s/pattern/pattern/p

如何结合多个条件和行范围?

文件示例:

For the first time since early February
the top three films at the weekend 
box office were all new releases
and leading the way was
Fox's Bohemian Rhapsody
with a $50 million
chart-topping performance
well above expectations.
That said, the weekend isn't all about
new releases as the holdovers

输出应该是:

 For the first time since early February
 the top three films at the weekend
 and leading the way was
 with a $50 million
 That said, the weekend isn't all about
 new releases as the holdovers

标签: sed

解决方案


  • 获取包含的行the

    sed -n '1,10{/\<the\>/p}' file
    
  • 获取包含theora或 or的行an

    sed -n '1,10{/\<\(the\|a\|an\)\>/p}' file
    

推荐阅读