in reply to how to change this code into perl

Something like this (assumes whitespace delimited fields):

perl -anle"next unless @L; print if $L[0] eq $F[0]; @L = @F;" in.txt > + out.txt

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!

Replies are listed 'Best First'.
Re^2: how to change this code into perl
by Laurent_R (Canon) on Aug 30, 2015 at 07:55 UTC
    I am afraid this:
    perl -anle"next unless @L; print if $L[0] eq $F[0]; @L = @F;" in.txt > + out.txt
    will never enter into the loop because the next statement at the beginning will prevent @L from ever being set.

    Perhaps this instead:

    perl -anwle 'BEGIN{$L = "";} print if $F[0] eq $L; $L = $F[0];' in.tx +t > out.txt
    Although the BEGIN block isn't really necessary if the warnings are not activated:
    perl -anle 'print if $F[0] eq $L; $L = $F[0];' in.txt > out.txt

        thank you BrowserUK, I am getting this error.

        syntax error at -e line 2, at EOF Execution of -e aborted due to compilation errors.

        I am pretty new to perl, what does the error mean. I googled but its not very clear

      Thank you Laurent_R!!! the one liner is not printing all the lines, say I have three duplicates its only printing the last two or one duplicate, not all of them.

      1 twenty 2 thirty 1 forty 1 fifty
      output 1 twenty 1 forty 1 fifty

      is there a way to script it instead of a oneliner. Thank you guys

        OK, a real script that should detect all lines having duplicate keys (quick script, untested, no time now, but based on something I am doing quite often, so, hopefully, I've it right).
        my ($previous_key, $previous_line); open my $IN, "<", $infile or die "cannot open $infile $!"; while (<$IN>) { my $key = $1 if /^(\w+)/; if ($key eq $previous_key) { print $previous_line if defined $previous_line; print $_; undef $previous_line; } else { $previous_line = $_; } $previous_key = $key; }
        Sure, where there are two entries with the same key, it only prints the second one (the duplicate, not the original one); when there are three, it will print only the second one and the third one. And of course, it will work only if the lines are properly sorted.

        If you need to print all the lines that are duplicates, then it is slightly more complicated, because you need to keep track of recent history. And then, yes, it is probably better to write a real script.

        Another way is to use a hash to keep track of everything in memory.

        the file will be around 20000 rows and the first columns will always be text..

        Hi poj, thank you for the quick response, I tried the script and could not get the duplicate rows, the outcome came up with zero rows. below is the script i tried

        open IN,'<','/home/scripts/imageoutcome.txt' or die "Could not open $i +nfile : $!"; my %count = (); my @lines = (); while (<IN>){ push @lines,$_; # print $_; if (/^(\S+)/){ ++$count{$1}; } } close IN; open OUT,'>','/home/scripts/outcome.txt' or die "Could not open $outfi +le : $!"; #print @lines; for (@lines){ if (/^(\S+)/){ print $count{$1}; print OUT $_ if $count{$1} > 0; } } close OUT;

        How big are the files and is the first column always numeric ?

        poj

        Thank you very much Laurent_R, I tried the script and its printing all the rows, instead of duplicates. Laurent_R, this code looks very interesting, can you please explain it

        #!/usr/bin/perl my ($previous_key, $previous_line); open my $IN, "<", '/home/scripts/imageoutcome.txt' or die "cannot open + $infile $!"; while (<$IN>) { my $key = $1 if /^(\w+)/; if ($key eq $previous_key) { print $previous_line if defined $previous_line; print $_; undef $previous_line; } else { $previous_line = $_; } $previous_key = $key; }

        Hi poj, you are correct, I forgot chomp, its working now. thank you so much for helping me.

        Hi Laurent_R, That was my bad, I had hidden characters in it, thats why I did not work. Your script is working...thank you so much for helping me and explaining it..