You're viewing a comment by Paul and its responses.

Paul Permalink
July 29, 2009, 16:25

Reply to Peter Passchier:
February 22nd, 2009 at 11:23 am
Peter came up with:
awk ‘BEGIN {srand();a=rand()} a!=$0 {a=$0;print}’

No need to use a random value (insecure too - Sun awk only uses 32768 distinct random values). Best initialiser for a variable is "\n", because that can never be in your input but you can stuff it in a string.

Alternative tool to deal with initial conditions is to factor in a test for first line: NR == 1

(I didn’t understand what the semicolon after a!=$0 does or means, so I removed it, and added a print action whenever the line isn’t equal to the previous.)

The ; is a trick. It makes the one-liner into two lines (!). Before the ";" is a match without an action, so it prints if true. After the ";" is an action without a match, so it always stores the value.

Reply To This Comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the first letter of your name: (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.