Today, My coworker asked me how to combine the same line in one file. Here is my answer:
cat tt.txt a a b c c b 123 3243 123 133 133 perl -mPerl6::Junction=all -lne 'our @c; print,push @c,$_ if $_ ne all +(@c);' tt.txt
a little ugly, but works.




I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: one line for combining line
by ikegami (Patriarch) on Jul 06, 2010 at 04:05 UTC

    If you don't care about order:

    $ sort -u tt.txt 123 133 3243 a b c

    If you do:

    $ perl -ne'$s{$_}++||print' tt.txt a b c 123 3243 133

    Same, but one char shorter: (requires Perl 5.10 or higher)

    $ perl -nlE'$s{$_}++||say' tt.txt a b c 123 3243 133

      Thanks!

      Now I know }{ and understand toolic's one line.But autovivification in your code confused me, does it mean returning FALSE when created autovivification? Any document for my reference?





      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

        Fetching a non-existent hash element returns undef. Is that what you meant? There's no autovivification. The hash elements are created when ++ assigns a value to them.

Re: one line for combining line
by toolic (Bishop) on Jul 06, 2010 at 03:52 UTC
    If you don't care about the order of the output, you could just use a hash instead of the Perl6::Junction CPAN module. How's this for ugly...
    perl -ne '$h{$_}++}{print for keys%h' tt.txt

    Update: not to mention sort...

    sort -u tt.txt
      Obfuscated code for me, absolutely! what does }{ mean? help! ;)




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

        What comes after ends up outside the -n loop.
        $ perl -MO=Deparse -ne'f()' LINE: while (defined($_ = <ARGV>)) { f(); } -e syntax OK $ perl -MO=Deparse -ne'f() }{ g()' LINE: while (defined($_ = <ARGV>)) { f(); } { g(); } -e syntax OK
Re: one line for combining line
by nyamned (Sexton) on Jul 13, 2010 at 17:25 UTC
    awk '!_[$0]++' tt.txt