awk - 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 '
( ((1 < $3) && ($3 < 1000)) || ((2000 < $3) && ($3 < 4000)) ) &&
sub(/[^\047]+\047/,"") && sub("patternid.*","") && !seen[$0]++
' file
Desc 1
Desc 2
Desc 3
Desc 4
推荐阅读
- go - Golang 可以使用字符串来执行操作吗?
- regex - VB.Net 用正则表达式提取数据
- azureportal - Azure 门户 (Chrome) 中的函数应用刀片空白
- javascript - Javascript 承诺中的 async/await
- javascript - 函数在两个不同的元素上执行不同
- php - 如何检查一个数组是否按顺序在php中包含另一个数组?
- c# - 如何处理从 C# 中的 C++ DLL 返回的 IntPtr?
- c# - MemCached 覆盖 dotnetcore 默认会话
- c# - 列表“GetEnumerator”错误中的 foreach 数据表
- c# - FB Messenger 上 JSON 中指定的 Botframework V4 错误解析类型