New answers tagged

0

Fun puzzle! In Perl: #! /usr/bin/env perl use strict; use warnings; my $prev; while (<>) { $prev = $_, next unless defined $prev; # prime the pump if ($prev ne $_) { print $prev; $prev = $_; # first half of a new pair } else { undef $prev; # discard and unprime the pump } } ...


0

a version: I use "delimiters" to simplify the inner loop (it assumes the first line is not __unlikely_beginning__ and it assumes the text is not ending with the line : __unlikely_ending__, and add that special delimiter line at the end of the inputed lines. Thus the algorithm can assume both: ) { cat INPUTFILE_or_just_- ; echo "__unlikely_ending__" ; } | ...


2

As I understood the question I opted for awk, using a hash of each record, in this case I'm assuming that RS=\n, but it can be changed to consider any other sort of arrangements, it can be arranged to consider an even number of reps, instead of the odd, with a parameter or a small dialog. Every line is used as the hash and its count is increased, at the end ...


3

I like python for this, for example with python 2.7+ from itertools import groupby with open('input') as f: for k, g in groupby(f): if len(list(g)) % 2: print(k),


3

if input is sorted: perl -0pe 'while(s/(.*)\n\1\n//){}'


4

With pcregrep for a given sample: pcregrep -Mv '(.)\n\1$' file or in a more general way: pcregrep -Mv '(^.*)\n\1$' file


6

I worked out the sed answer not long after I posted this question; no one else has used sed so far so here it is: sed '$!N;/^\(.*\)\n\1$/d;P;D' A little playing around with the more general problem (what about deleting lines in sets of three? Or four, or five?) provided the following extensible solution: sed -e ':top' -e '$!{/\n/!{N;b top' -e ...


4

Give a try to this awk script below: #!/usr/bin/awk -f { if ((NR!=1) && (previous!=$0) && (count%2==1)) { print previous; count=0; } previous=$0; count++; } END { if (count%2==1) { print previous; } } It is assumed that the lines.txt file is sorted. The test: $ chmod +x script.awk $ ./script.awk lines.txt a d e


1

with perl: uniq -c file | perl -lne 'if (m(^\s*(\d+) (.*)$)) {print $2 if $1 % 2 == 1}'


1

If input is sorted what about this awk: awk '{ x[$0]++; if (prev != $0 && x[prev] % 2 == 1) { print prev; } prev = $0; } END { if (x[prev] % 2 == 1) print prev; }' sorted


1

Using shell constructs, uniq -c file | while read a b; do if (( $a & 1 == 1 )); then echo $b; fi done


4

It's not very elegant, but it's as simple as I can come up with: uniq -c input | awk '{if ($1 % 2 == 1) { print substr($0, 9) }}' The substr() just trims off the uniq output. That'll work until you have more than 9,999,999 duplicates of a line (in which case uniq's output may spill over 9 characters).



Top 50 recent answers are included