bash 具有字段值条件的 linux cut 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30898094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:13:06  来源:igfitidea点击:

linux cut command with field-value condition

linuxbashawkfieldcut

提问by Ranjoy Saha

I am trying to figure out a linux shell command which will display portion of a field from a log satisfying condition of other field.

我试图找出一个 linux shell 命令,它将显示满足其他字段条件的日志中的字段的一部分。

Sample log-file:

示例日志文件

2013-04-15;[email protected];[email protected];outgoing;24;headline
2013-04-15;[email protected];[email protected];incoming;0;chat
2013-04-15;[email protected];[email protected];outgoing;26;headline
2013-04-15;[email protected];333333;incoming;12;chat
2013-04-16;[email protected];555555;incoming;0;chat

I tried to display only the unique numbers from field 2 and 3 separated by delimiter ';' where field 5 != 0

我试图只显示字段 2 和 3 中由分隔符“;”分隔的唯一数字 其中字段 5 != 0

Sample expected output:

示例预期输出:

111111
333333
444444

采纳答案by karakfa

$ awk -F";" '!=0{print , }' fields.txt | grep -o '[0-9]\+' | sort -u
111111
333333
444444

Explanation: extract columns 2 and 3 based on column 5 value; match only the numbers; eliminate duplicates

说明:根据第5列的值提取第2列和第3列;只匹配数字;消除重复

回答by adelphus

grep -Po '\d+(?=([^;]*;)?[^;]*;[^;]+;[^0][^;]*;[a-z]+$)' logfile|sort -u

Should be self-explanatory. lol.

应该是不言自明的。哈哈。