使用awk在匹配单词之前和之后打印'x'个单词

时间:2021-08-13 16:03:48

I am searching for a particular word in a text file. The below command is returning every occurrence in that text file

我正在搜索文本文件中的特定单词。以下命令将返回该文本文件中的每个匹配项

awk '/text_to_be_searched/ { match($0, /text_to_be_searched/); print ( NR-1"-----"$0) ;}' filename.txt 

But now I have to display only 'x' number of words before and after the matched word for the first occurrence.

但是现在我必须在匹配的单词之前和之后仅显示第一次出现的'x'个单词。

2 个解决方案

#1


1  

You can use grep instead

你可以改用grep

grep -Eo -m 1 '.{x}pattern.{y}' pattern files

x is the no of words to match before pattern

x是模式之前匹配的单词

y is the no of words to match after pattern

y是模式后匹配的单词

-m 1 means return the first match

-m 1表示返回第一场比赛

Note: any regex special characters in pattern should be escaped or else it would be interpreted as regex

注意:模式中的任何正则表达式特殊字符都应该被转义,否则它将被解释为正则表达式

#2


0  

awk to the rescue

拯救

awk -v word="your word here" '{
     for(i=1;i<=NF;i++) 
        if(match($i,word)) 
         {m=i; break}
     } 
     m {
       for(i=m-3;i<=m+3;i++) 
          if(i>0 && i<=NF) print $i; 
       exit
     }'

will print 3 words before and after the first match if there is one. The context of this span is a line. If you want to expand to full paragraph add -vRS=

如果有的话,将在第一场比赛前后打印3个单词。这个范围的背景是一条线。如果要扩展到完整段落,请添加-vRS =

#1


1  

You can use grep instead

你可以改用grep

grep -Eo -m 1 '.{x}pattern.{y}' pattern files

x is the no of words to match before pattern

x是模式之前匹配的单词

y is the no of words to match after pattern

y是模式后匹配的单词

-m 1 means return the first match

-m 1表示返回第一场比赛

Note: any regex special characters in pattern should be escaped or else it would be interpreted as regex

注意:模式中的任何正则表达式特殊字符都应该被转义,否则它将被解释为正则表达式

#2


0  

awk to the rescue

拯救

awk -v word="your word here" '{
     for(i=1;i<=NF;i++) 
        if(match($i,word)) 
         {m=i; break}
     } 
     m {
       for(i=m-3;i<=m+3;i++) 
          if(i>0 && i<=NF) print $i; 
       exit
     }'

will print 3 words before and after the first match if there is one. The context of this span is a line. If you want to expand to full paragraph add -vRS=

如果有的话,将在第一场比赛前后打印3个单词。这个范围的背景是一条线。如果要扩展到完整段落,请添加-vRS =