[英]Print unique lines, compare no more than N characters
With uniq
, you can choose to compare only first N
characters 使用
uniq
,您可以选择仅比较前N
字符
$ cat foo.txt The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy cat. The quick brown fox jumps over the lazy mouse. $ uniq -w 40 foo.txt The quick brown fox jumps over the lazy dog.
Can the same effect be achieved using awk
? 使用
awk
可以达到相同的效果吗? I read this example 我读了这个例子
awk '!a[$0]++'
but it compares the whole line. 但它会比较整行。
awk has substr()
function: awk具有
substr()
函数:
awk '!a[substr($0,1,40)]++'
with your example: 与您的示例:
kent$ echo "The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy mouse."|awk '!a[substr($0,1,40)]++'
The quick brown fox jumps over the lazy dog
Two alternatives using FIELDWIDTHS
and FPAT
: 使用
FIELDWIDTHS
和FPAT
两种选择:
awk '!a[$1]++' FIELDWIDTHS=40
awk '!a[$1]++' FPAT='.{40}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.