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

0 bash awk

使用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.

可以使用相同的效果awk吗?我读了 这个例子

awk '!a[$0]++'
Run Code Online (Sandbox Code Playgroud)

但它比较整条线.

Ken*_*ent 11

awk有substr()功能:

awk '!a[substr($0,1,40)]++'
Run Code Online (Sandbox Code Playgroud)

用你的例子:

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
Run Code Online (Sandbox Code Playgroud)