【问题标题】:Print unique specific text form a line if it meet the search criteria如果符合搜索条件,则打印一行唯一的特定文本
【发布时间】:2021-05-17 00:04:04
【问题描述】:

我有以下 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 -F"service commands |description |'" '/service commands/ && /description /{for(i=1; i<=NF; ++i) printf"%s", $i ; print""}' input.txt

但这不是我想要的

我的愿望输出是

Desc 1
Desc 2
Desc 3
Desc 4

我怎样才能用一个 awk 命令或它们的组合来做到这一点?

【问题讨论】:

    标签: awk


    【解决方案1】:
    $ 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
    

    【讨论】:

    • 感谢您的解决方案。您能否在“服务命令”和“描述”的行模式中添加搜索,因为我的文件有其他命令和其他行,我只需要在包含这些单词的行中搜索。我也可以一次搜索一个标准,例如1-1000
    • @swa056 我发布的脚本会根据您提供的输入生成您要求的输出。如果您的问题中发布的示例输入和预期输出不足以测试您的要求,请更新它们。关于“还有……”——是的,一个条件比多个条件更容易搜索。考虑到所有这些,您应该只问一个新的后续问题,而不是更改此问题,因为强烈建议不要使用 Chameleon Questions
    • 对不起,我没有从一开始就给出我所有的主张。您的答案完美无缺,我设法仅使用一个搜索条件对其进行了自定义。但是因为有可能对此进行错误搜索,所以我要求能够添加搜索模式以消除任何错误情况,但这并不意味着您的答案不起作用。再次感谢您的解决方案,我将针对模式搜索提出一个新问题。
    【解决方案2】:

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

    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
    

    说明:检查是否在当前行中找到正则表达式模式[0-9]+ description.*Desc[[:space:]]+[0-9]+[[:space:]]+patternid(根据 OP 的要求),然后检查条件,第三个字段值是否在 1 的范围内到 1000 或第三个字段范围是从 2000 到 4000 然后检查一个条件,如果数组中不存在第 5 个和第 6 个字段组合,然后打印该值(以在输出中获取唯一值)。



    第二个解决方案:

    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
    

    【讨论】:

    • 感谢您的回答,但我的问题是我想要获得的值并不总是以“Desc”和数字开头,但它可以是任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 2013-09-06
    相关资源
    最近更新 更多