Awk 如果符合搜索条件,则以行形式打印唯一的特定文本

Awk 如果符合搜索条件,则以行形式打印唯一的特定文本,awk,Awk,我有以下input.txt文件 service commands 1 description 'Desc 1 patternid com3' service commands 2 description 'Desc 1 patternid com3 from err1' service commands 3 description 'Desc 2 patternid com3 from err2' service commands 4 description 'Desc 2 patternid

我有以下input.txt文件

service commands 1 description 'Desc 1 patternid com3'
service commands 2 description 'Desc 1 patternid com3 from err1'
service commands 3 description 'Desc 2 patternid com3 from err2'
service commands 4 description 'Desc 2 patternid com3 from err3'

service commands 1000 description 'to value1 patternid from 1000'
service commands 1001 description 'to value1 patternid from 1001'

service commands 2000 description 'Desc 3 patternid com'
service commands 2001 description 'Desc 3 patternid com2 from err1'
service commands 2002 description 'Desc 4 patternid com2'
service commands 2003 description 'Desc 4 patternid com2 from err1'

service commands 4000 description 'output patternid to com1'
service commands 5000 description 'input to com1'
service commands 6000 description 'input to com2'
我希望使用awk从服务命令编号满足1-1000和2000-4000之间标准的行中获取唯一的描述 但是我只需要patternid之前的描述文本

我在搜索中找到了这个

awk -F'_' '($1>1999 && $1<4000){print}' input.txt
如何使用一个awk命令或它们的组合来执行此操作?

第一种解决方案:仅使用所显示的示例,请尝试以下操作

awk '
/[0-9]+ description.*Desc[[:space:]]+[0-9]+[[:space:]]+patternid/ && (($3>=1 && $3<=1000) || ($3>=2000 && $3<=4000)) && !arr[$5,$6]++{
  sub(/^\047/,"",$5)
  print $5 OFS $6
}
'  Input_file
说明:为上述内容添加详细说明以下内容仅用于说明目的,用于运行上述代码

第一种解决方案:仅凭所展示的样品,请尝试以下方法

awk '
/[0-9]+ description.*Desc[[:space:]]+[0-9]+[[:space:]]+patternid/ && (($3>=1 && $3<=1000) || ($3>=2000 && $3<=4000)) && !arr[$5,$6]++{
  sub(/^\047/,"",$5)
  print $5 OFS $6
}
'  Input_file
说明:为上述内容添加详细说明以下内容仅用于说明目的,用于运行上述代码


谢谢你的解决方案。您是否可以添加服务命令和描述的行搜索模式,因为我的文件中有和其他命令的行,并且我只需要在包含这些单词的行中搜索。我还可以一次搜索一个标准,例如1-1000@swa056我发布的脚本根据您提供的输入生成您要求的输出。如果您问题中发布的示例输入和预期输出不足以测试您的需求,请更新它们。关于并且也是的,一个条件比多个条件更容易搜索。考虑到所有这些,你不应该改变这个问题,你应该问一个新的后续问题,因为你非常不受鼓励。对不起,我没有从一开始就给出我所有的要求。你的答案非常完美,我只使用了一个搜索条件就对它进行了定制。但是,因为这有可能是一个错误的搜索,我要求能够添加一个搜索模式,以消除任何情况下的错误,但这并不意味着你的答案不工作。再次感谢您的解决方案,我将为模式搜索提出一个新问题。感谢您的解决方案。您是否可以添加服务命令和描述的行搜索模式,因为我的文件中有和其他命令的行,并且我只需要在包含这些单词的行中搜索。我还可以一次搜索一个标准,例如1-1000@swa056我发布的脚本根据您提供的输入生成您要求的输出。如果您问题中发布的示例输入和预期输出不足以测试您的需求,请更新它们。关于并且也是的,一个条件比多个条件更容易搜索。考虑到所有这些,你不应该改变这个问题,你应该问一个新的后续问题,因为你非常不受鼓励。对不起,我没有从一开始就给出我所有的要求。你的答案非常完美,我只使用了一个搜索条件就对它进行了定制。但是,因为这有可能是一个错误的搜索,我要求能够添加一个搜索模式,以消除任何情况下的错误,但这并不意味着你的答案不工作。再次感谢您的解决方案,我将为模式搜索提出一个新问题。感谢您的回答,但我的问题是,我想要获取的值并不总是以单词Desc和数字开头,但它可以是任何内容。感谢您的回答,但我的问题是,我想要获取的值并不总是以单词Desc开头和一个数字,但它可以是任何东西。
awk '
/[0-9]+ description.*Desc[[:space:]]+[0-9]+[[:space:]]+patternid/ && (($3>=1 && $3<=1000) || ($3>=2000 && $3<=4000)) && !arr[$5,$6]++{
  sub(/^\047/,"",$5)
  print $5 OFS $6
}
'  Input_file
awk '
match($0,/^service commands [0-9]+[[:space:]]+description.*Desc [0-9]+ patternid/) && (($3>=1 && $3<=1000) || ($3>=2000 && $3<=4000)){
  val=substr($0,RSTART,RLENGTH)
  sub(/.*Desc/,"Desc",val)
  sub(/ patternid/,"",val)
  if(!arr[val]++){ print val }
}'  Input_file
# Starting awk program from here.
awk '

    # Using match function to match regex to make sure line has exact format
    # needed by OP. Then checking conditions either 3rd field value comes in
    # range of 1 to 1000 OR 3rd field range is coming from 2000 to 4000.
    match($0, /^service commands [0-9]+[[:space:]]+description.*Desc [0-9]+ patternid/) &&
            (($3 >= 1 && $3 <= 1000) || ($3 >= 2000 && $3 <= 4000)) {

        # Creating val which has sub string of matched regex.
        val = substr($0, RSTART, RLENGTH)

        # Globally substituting everything till Desc with Desc in val here.
        gsub(/.*Desc/, "Desc", val)

        # Substituting space patternid with NULL in val here.
        sub(/ patternid/, "", val)

        # Checking condition if entry is not already present in arr then print val here.
        if ( !seen[val]++ ) {
            print val
        }
    }

# Mentioning Input_file name here.
' Input_file
$ awk '
    ( ((1 < $3) && ($3 < 1000)) || ((2000 < $3) && ($3 < 4000)) ) &&
    sub(/[^\047]+\047/,"") && sub("patternid.*","") && !seen[$0]++
' file
Desc 1
Desc 2
Desc 3
Desc 4