首页 > 解决方案 > Print unique specific text form a line if it meet the search criteria

问题描述

I Have the following input.txt file

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'

I want with awk to take the unique descriptions from lines where the service commands number meet the criteria between 1-1000 and 2000-4000 But I want only the description text that is before the word "patternid"

I have found this for the search

awk -F'_' '($1>1999 && $1<4000){print}' input.txt

But I can't make to search in 3nd filed and for multiple values

I can print the description with

awk -F"service commands |description |'" '/service commands/ && /description /{for(i=1; i<=NF; ++i) printf"%s", $i ; print""}' input.txt

But this not what i want

My desire output is

Desc 1
Desc 2
Desc 3
Desc 4

How can I do it with one awk command or a combination of them?

标签: awk

解决方案


$ 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

推荐阅读