Print unique lines, compare no more than N characters

With uniq, you can choose to compare only first N characters

$ 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? I read this example

awk '!a[$0]++'

but it compares the whole line.

Answers


awk has substr() function:

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

Need Your Help

An issue with MySQL query in php

php mysql codeigniter

$users_id = $this->db->query('SELECT users_id FROM users ORDER BY daily_points desc limit 1');

XML Android actionbar Item dropdown options

java android xml menu android-actionbar

I have a item in my actionbar, when i click on it I want to have 2 options to chose from.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.