29 users online (1 members and 28 guests)  

Thread: Bash <> Perl


  Results 1 to 10 of 10

Related

  1. Need help with perl    Forum: CGI Perl Forum
    Replies: 1
  2. Replies: 1
  3. Replies: 1
  4. Replies: 0
  1. #1
    MiH's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2006
    Posts
    5

    Bash <> Perl

    I have a script written in bash but I would like to convert it into Perl.

    I'm stuck at this line :

    CARDS_NUMBER=`cat /root/service/conf.info |wc -l`

    Is there any solution to transform it in to Perl ?

  2. #2
    jkk's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2006
    Posts
    8

    Thumbs up Re: Bash <> Perl

    you can use backtics to do it as a system call:
    my $count = `wc -l < conf.info`;
    print $count;
    or
    open(FILE, "conf.info");
    my $count;
    $count++ while <FILE>;
    close(FILE);
    print $count;

  3. #3
    MiH's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2006
    Posts
    5

    Re: Bash <> Perl

    THX jkk works geat

    any change to configure something like this to in perl :

    CRYPTO=`cat /root/conf.info |awk '{print $6}' | cut -b -2 |grep 0D |wc -l`

  4. #4
    jkk's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2006
    Posts
    8

    Re: Bash <> Perl

    Are you looking for pattern 0D in grep??? maybe this is what you want???
    #!/usr/bin/perl -Tw
    open(FH, "<conf.info");
    my $count;
    while(<FH>){
    my $var = substr((split(" ", $_))[5], 0, 2);
    if($var =~ /0D/){
    $count++;
    print "$var\n";
    }
    }
    print "$count\n";
    close(FH);

  5. #5
    MiH's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2006
    Posts
    5

    Re: Bash <> Perl

    Hi jkk,

    Yes i want to grep 0D but only when it is at a certain position ( like )

    0DIRK.####.## Card 0D002000 Sl:3 Lev:2 dist:3 id:0D56

    you see 3 x 0D but he only may count the 0D from 0D002000

  6. #6
    jkk's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2006
    Posts
    8

    Re: Bash <> Perl

    So if you change the [5] to [2], then you should be checking the first two chars of the 3rd " " delimited field.
    my $var = substr((split(" ", $_))[5], 0, 2);
    you could also change if($var =~ /0D/){
    to
    if($var eq "0D"){ ... since you have taken these first two characters.
    BETTER YET...
    if((split(" ", $_))[2] =~ /^0D/){
    which tells it to look for the 0D pattern at the beginning of the third field.

  7. #7
    MiH's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2006
    Posts
    5

    Re: Bash <> Perl

    You are GREAT jkk

    is there also a possibility to sort unieke id's ?

    example :

    0DIRK.####.## Card 0D002000 Sl:3 Lev:2 dist:3 id:0D56
    0DIRK.####.## Card 0D002002 Sl:3 Lev:2 dist:3 id:0D56
    0DIRK.####.## Card 0D002001 Sl:3 Lev:2 dist:3 id:0D56
    0DIRK.####.## Card 0D002000 Sl:3 Lev:2 dist:3 id:0D56
    0DIRK.####.## Card 0D002000 Sl:3 Lev:2 dist:3 id:0D56
    0DIRK.####.## Card 0D002000 Sl:3 Lev:2 dist:3 id:0D56

    CARDS_CAIDS = `cat conf.info" |awk '{print $3}' |sort |uniq |wc -l`;

    if you count all 0D002000 / 0D002002 etc we have 6. but in fact we have 3 different and 3 the same so sorted out it's 3

    This may not be done at 0D but at the whole 3 line because it also could be :

    0DIRK.####.## Card 1702200 Sl:3 Lev:2 dist:3 id:0D56

    now we have 7 lines ( with this one ) and sorted out it would be 4

  8. #8
    jkk's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2006
    Posts
    8

    Re: Bash <> Perl

    make use of a hash and array (this is in the O'reilly Perl Cookbook).
    my %seen = ();
    my @unique_array = ();
    # as you read thru and pull this field...
    push(@unique_array, $val) unless $seen{$item}++;

  9. #9
    MiH's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2006
    Posts
    5

    Re: Bash <> Perl

    make use of a hash and array (this is in the O'reilly Perl Cookbook).
    did order the cookbook today jkk try to figger it all out when it comes in. This is the last problem in my file after that ot should be up and running.

    THX for @ll the help.

  10. #10
    jkk's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2006
    Posts
    8

    Re: Bash <> Perl

    You're quite welcome. The Cookbook gives a lot of good examples which you can take and make modifications to in order to figure some of the stuff out. Good luck!



Tags for this Thread