简体   繁体   English

打印唯一的行,比较不超过N个字符

[英]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 : 使用FIELDWIDTHSFPAT两种选择:

awk '!a[$1]++' FIELDWIDTHS=40

awk '!a[$1]++' FPAT='.{40}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM