Skip to content

Instantly share code, notes, and snippets.

@cosmin
Created January 16, 2011 15:20
Show Gist options
  • Select an option

  • Save cosmin/781874 to your computer and use it in GitHub Desktop.

Select an option

Save cosmin/781874 to your computer and use it in GitHub Desktop.
print only lines with unique fields, keep track of count
#one-liner awk 'NR == 1 {lastfield = $1; matchline = $0; count = 1; next} {if (lastfield != $1) {print count " " matchline; count = 0; matchline=$0; } count += 1; lastfield = $1} END {print count " " matchline}' inp.txt
NR == 1 {
lastfield = $1;
matchline = $0;
count = 1;
next
}
{
if (lastfield != $1) {
print count " " matchline;
count = 0;
matchline=$0;
}
count += 1;
lastfield = $1
}
END {
print count " " matchline
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment