如何使用Awk输出多个连续行

如何使用Awk输出多个连续行,awk,Awk,输入/文件 A:1111 B:21222 C:33rf33 D:444dct4 E:5tdffe F:4444we G:j5555 H:46666 I:efe989ef J:efee 基本上需要选择包含2122的行(即B/2行) &从444dct4(即D线)开始到efe989ef(即i/9线)的线路 概括 Select Line B (contains 2122) Select Line D (444dct4) till Line I 期望输出 B:21222 D:444dct4 E:5td

输入/文件

A:1111
B:21222
C:33rf33
D:444dct4
E:5tdffe
F:4444we
G:j5555
H:46666
I:efe989ef
J:efee
基本上需要选择包含2122的行(即B/2行) &从444dct4(即D线)开始到efe989ef(即i/9线)的线路 概括

Select Line B (contains 2122)
Select Line D (444dct4) till Line I
期望输出

B:21222
D:444dct4
E:5tdffe
F:4444we
G:j5555
H:46666
I:efe989ef
为了灵活性,将要查找的行设置为str1,将from设置为str2,将to设置为str3。设置打印标志(标志)以开始。当2122位于第二个字段时,打印。然后,当第二个字段以44dct4开头时,将打印标志设置为1。当第二个字段以efe989ef开头时,将打印标志设置为0,打印该行并跳到下一条记录。然后,变量标志将决定打印什么和不打印什么

$ awk -F: '
/2122/ {                      # line that contains 2122
    print                  
    next                      # to avoid duplicate printing if 2122 also in D-I
}
$2~/^444dct4/,$2~/efe989ef/   # starts with 444dct4 till efe989ef
' file
输出:

B:21222
D:444dct4
E:5tdffe
F:4444we
G:j5555
H:46666
I:efe989ef
编辑

一艘班轮:

$ awk -F: '/2122/{print; next} $2~/^444dct4/,$2~/efe989ef/' file.txt 

请您尝试使用GNU
awk
中显示的样本编写并测试以下内容。如果行的第2列
21222
介于
444dct4
efe989ef
之间,则该行也会注意,这样它就不会重新打印它

awk -F':' '
$2=="21222" && !found{
  print
  next
}
$2=="444dct4"{
  found=1
}
found
$2=="efe989ef"{
  found=""
}
'  Input_file
说明:添加上述内容的详细说明

awk -F':' '              ##Starting awk program from here and setting field separator as colon here.
$2=="21222" && !found{   ##Checking if 2nd field is 21222 and found is NOT set then try following.
  print                  ##Printing the current line here.
  next                   ##next will skip all further statements from here.
}
$2=="444dct4"{           ##Checking condition if 2nd field is 444dct4 then do following.
  found=1                ##Setting found to 1 here.
}
found                    ##Checking condition if found is SET then print that line.
$2=="efe989ef"{          ##Checking condition if 2nd field is efe989ef then do following.
  found=""               ##Nullifying found here.
}
'  Input_file            ##Mentioning Input_file name here.

欢迎来到SO,请在您的问题中添加您的努力,这是非常令人鼓舞的。另外,请在您的问题中更清楚地解释获得预期输出的逻辑。我认为这个问题是重复的,您想要包含2122的行还是(即B行或2行)谢谢James。包含2122@KarimTawfik如果我理解正确的话,我想这个问题与两个单独的文件有关。我需要一个文件&需要所需的outputawk-F:'/2122/{printnext}$2~/^444dct4/,$2~/efe989ef/'file.txtOutput从D开始,直到I结束。在输出中看不到B
/2122/{printnext}
需要a
中间:
/2122/{print;next}
谢谢。我试过了,但没有结果。您可以共享适用于的命令吗you@PrasadV,此解决方案是经过测试的解决方案,与您展示的样品配合使用,效果良好。您的实际输入文件是否与所示示例相同?谢谢。我没法让它工作。您能否共享适用于您的命令?与上面相同的命令。我使用了您发布的示例示例。我还验证了输出是否符合要求。是的,可以。我尝试使用我的实际输入文件(未在此处发布)&需要帮助。你发过电子邮件吗?给谁发邮件?在你的网站上发邮件
awk -F':' '              ##Starting awk program from here and setting field separator as colon here.
$2=="21222" && !found{   ##Checking if 2nd field is 21222 and found is NOT set then try following.
  print                  ##Printing the current line here.
  next                   ##next will skip all further statements from here.
}
$2=="444dct4"{           ##Checking condition if 2nd field is 444dct4 then do following.
  found=1                ##Setting found to 1 here.
}
found                    ##Checking condition if found is SET then print that line.
$2=="efe989ef"{          ##Checking condition if 2nd field is efe989ef then do following.
  found=""               ##Nullifying found here.
}
'  Input_file            ##Mentioning Input_file name here.