<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chan Chen Coding...

    Perl Print Duplicate Line

    # Find out duplicate line, if yes, print it out.
    AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY
    LC_MESSAGES
    AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
    AcceptEnv LC_IDENTIFICATION LC_ALL
     
    # Example of overriding settings on a per-user basis
    #Match User anoncvs
    # X11Forwarding no
    # AllowTcpForwarding no
    # ForceCommand cvs server

    AcceptEnv LC_IDENTIFICATION LC_ALL

    #! /usr/bin/perl
    use strict;

    open(FH, 'dupLine.sample');
    my %seen;
    while (<FH>) {
      $seen{$_}++;
    }
     
    while (my ($line$count) = each %seen) {
      print "$count: $line" if $count > 1;
    }


    Using the standard Perl shorthands:

    my %seen;
    while ( <> ) {
       
    print if $seen{$_}++;
    }

    As a "one-liner":

    perl -ne 'print if $seen{$_}++'

    More data? This prints <file name>:<line number>:<line>:

    perl -ne 'print ( $ARGV eq "-" ? "" : "$ARGV:" ), "$.:$_" if $seen{$_}++'

    Explanation on %seen:

    • %seen declares a hash. For each unique line in the input $seen{$_} is a scalar slot in the hash named by the the text of the line.
    • Using the postfix increment operator (x++) we take the value for our expression, remembering toincrement it after the expression. So, if we haven't "seen" the line $seen{$_} is undefined--but when forced into an numeric "context" like this, it's taken as 0--and false.
    • Then it's incremented to 1.

    So the first time we see a line, we take the undefined value which fails the if. It increments the count at the slot to 1. Thus, it is 1 for any future occurrences at which point it passes the if condition.

    Now as I said above, %seen declares a hash, but with strict turned off, any variable expression can be created on the spot. So the first time perl sees $seen{$_} it knows that I'm looking for %seen, it doesn't have it, so it creates it.

    An added neat thing about this is that at the end, if you care to use it, you have a count of how many times each line was repeated.



    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-04-26 11:01 Chan Chen 閱讀(295) 評論(0)  編輯  收藏 所屬分類: Linux

    主站蜘蛛池模板: 久久久久亚洲AV成人网| 男人的天堂亚洲一区二区三区 | 一级特黄aa毛片免费观看| 亚洲一区视频在线播放| 国产免费久久精品丫丫| 国产av无码专区亚洲av果冻传媒| 美女裸体无遮挡免费视频网站| 国产精品二区三区免费播放心| 久久人午夜亚洲精品无码区 | 免费一级毛片一级毛片aa| 国产亚洲精品国产福利在线观看 | 亚洲一区二区三区偷拍女厕| a级毛片黄免费a级毛片| 久久久久亚洲精品成人网小说| 久久久久久成人毛片免费看| 亚洲视频中文字幕在线| 久久久久久99av无码免费网站| 亚洲色丰满少妇高潮18p| 免费在线观看亚洲| WWW免费视频在线观看播放| 亚洲欧洲自拍拍偷午夜色无码| 久久久久久久久久国产精品免费 | 中文成人久久久久影院免费观看| 亚洲欧洲免费视频| 无码一区二区三区免费视频| 美女被爆羞羞网站免费| 亚洲一区无码中文字幕| 4399影视免费观看高清直播| 国内精品久久久久影院亚洲 | 日韩中文字幕精品免费一区| 亚洲aⅴ无码专区在线观看春色 | 亚洲精品中文字幕无乱码麻豆| 又粗又硬又黄又爽的免费视频| 不卡视频免费在线观看| 亚洲人成激情在线播放| 免费在线观看亚洲| 最近免费中文字幕mv电影| 久久亚洲精品无码网站| 亚洲Aⅴ无码专区在线观看q| 久久久久国色AV免费看图片 | 免费国产怡红院在线观看|