Bash 多个字符串按相同顺序匹配时如何获取行号

Bash 多个字符串按相同顺序匹配时如何获取行号,bash,shell,awk,sed,grep,Bash,Shell,Awk,Sed,Grep,我有一个文件,其中多次出现字符串test1和test2。 我试图找到匹配的行号,并根据它们出现的顺序打印这些行号。每个字符串在一行中显示一次 以下是一个例子: cat input.txt this is test1 this is not this is test2 this is test1 我天真的尝试获取行号和顺序是 grep -n 'test1' input.txt | cut -d : -f1 > output1.txt grep -n 'test2' input.txt

我有一个文件,其中多次出现字符串test1和test2。 我试图找到匹配的行号,并根据它们出现的顺序打印这些行号。每个字符串在一行中显示一次

以下是一个例子:

cat input.txt
this is test1
this is not
this is test2
this is test1
我天真的尝试获取行号和顺序是

grep -n 'test1' input.txt  | cut -d : -f1 > output1.txt
grep -n 'test2' input.txt  | cut -d : -f1 >> output1.txt
sort -k1n output1.txt
它的输出是

cat output1.txt
1
3
4
然后使用do while循环进行打印

while read line; do
 if [[ $line =~ test1 || $line =~ test2 ]] ; then
 echo $line >> output2.txt;
done <input.txt
我的问题是,对于这个解决方案,是否有更好、可能更有效的方法,特别是按照正确的顺序获取行号。 谢谢。

第一种解决方案:请尝试以下方法。它只会将行号放入output1.txt输出文件中

awk '/this is test[0-9]+/{print FNR}' Input_file > "output1.txt"
要获取不同输出文件output1.txt中的行号和内容,请尝试以下操作

awk '/this is test[0-9]+/{print FNR > "output1.txt";print $0 > "output2.txt"}' Input_file
grep -n 'this is test1\|this is test2' Input_file | cut -d':' -f2 > "output2.txt"
第二种解决方案:或者从@kamil cuk的评论中汲取灵感,稍加改进,只获得行号

grep -n 'test1\|test2' Input_file | cut -d':' -f1 > "output1.txt"
OR
grep -n 'this is test1\|this is test2' Input_file | cut -d':' -f1 > "output1.txt"
要将匹配的内容放入输出文件,请尝试以下操作

awk '/this is test[0-9]+/{print FNR > "output1.txt";print $0 > "output2.txt"}' Input_file
grep -n 'this is test1\|this is test2' Input_file | cut -d':' -f2 > "output2.txt"
第三种解决方案:使用sed:

要仅获取行号,请使用:

sed -n '/test[12]/{=;}'  Input_file > "output1.txt"
要获取行内容,请执行以下操作:

sed -n '/test[12]/p' Input_file > "output2.txt"
$ grep -nE 'test1|test2' input.txt    
1:this is test1                       
3:this is test2                       
4:this is test1                       

$ grep -nE 'test[12]' input.txt       
1:this is test1                       
3:this is test2                       
4:this is test1              
grep本身可以做到这一点,为什么还要麻烦呢

$ grep -E 'test1|test2' input.txt     
this is test1                         
this is test2                         
this is test1                         
如果需要行号和内容:

sed -n '/test[12]/p' Input_file > "output2.txt"
$ grep -nE 'test1|test2' input.txt    
1:this is test1                       
3:this is test2                       
4:this is test1                       

$ grep -nE 'test[12]' input.txt       
1:this is test1                       
3:this is test2                       
4:this is test1              
或者grep'test[12]'input.txt和grep-n'test[12]'input.txt

一种方法是:

仅适用于行号:

sed -n '/test[12]/=' input.txt
使用awk的优点是,它可以在一个命令内将不同的结果写入文件:

awk '/test[12]/{
    print FNR >"output1.txt"         #line numbers to output1.txt
    print >"output2.txt"             #contents to output2.txt
    print FNR ":" $0 >"output3.txt"  #both to output3.txt
}' input.txt

您期望的输出是什么?grep-n'test1\| test2'?output1.txt和output2.txt是精确的输出。我只是想知道是否有更好的方法获得output1.txtI更喜欢grep解决方案,因为在awk的情况下,它仅限于此特定示例。例如,取两个随机字符串testone和testtwo。@Boogeyman,很高兴它能帮助您,干杯。在awk解决方案中,优点是我没有硬编码任何test1或test2字符串,所以更多的字符串会出现,例如->test3、test4等,如果您需要了解所有内容,它们也会被忽略。