Perl script to replace awk

Discussion in 'Perl Misc' started by laura.hradowy@mts.NOSPAM.ca, Jul 26, 2004.

  1. Guest

    I and switching over to perl and need a starting point and need to write a
    script that will do the following...
    I just need a kick, and should be on my way.

    I have a file that is separated by commas, if I print the file out I would
    get this...
    awk -F, '{print "204"$1","$2","$3}' FILE

    The output is
    204xxxx001,00 0 05 21,TELN NOT
    204xxxx002,00 0 01 30,TELN NOT
    204xxxx008,00 0 04 15,TELN NOT
    204xxxx013,00 0 02 30,CUST HAS
    204xxxx015,00 0 10 22,CUST HAS

    I also need to insert data into this line...
    204xxxx001,EAST_BLD,ROOM2,00 0 05 21
    204xxxx002,EAST_BLD,ROOM2,00 0 01 30

    I am just having a difficult time trying to take awk and convert to perl.
    I know I can write in bash but I would like to try in perl.

    How do I prompt user for input that will insert into the $2, as well as
    $3.

    Basically I want,
    Enter BLD:
    User enter is EAST_BLD

    Enter room:
    User enters in ROOM2

    Then the perl script takes that info and inserts these fields into every
    line, as well as adding 204 at the begining and dumping the last field.

    And here is a trickier question...
    How do I do a grep on CUST and then place those lines in a file
    , Jul 26, 2004
    #1
    1. Advertising

  2. Bob Walton Guest

    wrote:

    ....
    > I am just having a difficult time trying to take awk and convert to perl.
    > I know I can write in bash but I would like to try in perl.

    ....


    Check out:

    perldoc a2p

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Jul 26, 2004
    #2
    1. Advertising

  3. <> wrote:


    > I am just having a difficult time trying to take awk and convert to perl.



    Run it through the awk-to-perl translator (a2p) that comes with perl itself.

    man a2p


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jul 26, 2004
    #3
  4. wrote:
    > I and switching over to perl and need a starting point and need to write a
    > script that will do the following...
    > I just need a kick, and should be on my way.
    >
    > I have a file that is separated by commas, if I print the file out I would
    > get this...
    > awk -F, '{print "204"$1","$2","$3}' FILE
    >
    > The output is
    > 204xxxx001,00 0 05 21,TELN NOT
    > 204xxxx002,00 0 01 30,TELN NOT
    > 204xxxx008,00 0 04 15,TELN NOT
    > 204xxxx013,00 0 02 30,CUST HAS
    > 204xxxx015,00 0 10 22,CUST HAS
    >
    > I also need to insert data into this line...
    > 204xxxx001,EAST_BLD,ROOM2,00 0 05 21
    > 204xxxx002,EAST_BLD,ROOM2,00 0 01 30
    >
    > I am just having a difficult time trying to take awk and convert to perl.
    > I know I can write in bash but I would like to try in perl.
    >
    > How do I prompt user for input that will insert into the $2, as well as
    > $3.
    >
    > Basically I want,
    > Enter BLD:
    > User enter is EAST_BLD
    >
    > Enter room:
    > User enters in ROOM2
    >
    > Then the perl script takes that info and inserts these fields into every
    > line, as well as adding 204 at the begining and dumping the last field.
    >
    > And here is a trickier question...
    > How do I do a grep on CUST and then place those lines in a file
    >
    >
    >

    Hi Laura,

    You could try this


    #!/usr/bin/perl
    print "Enter BLD:\n";
    chomp($input1 = <STDIN>);
    print "Enter room:\n";
    chomp($input2 = <STDIN>);
    open(TMP, "yourdatafile") or die $!;
    while(<TMP>){
    chomp;
    @L = split /,/, $_;
    print join(",", $L[0],$input1,$input2,@L[1..$#L]), "\n";
    }






    --


    Logidox Computing
    CM (Clearcase) & DataWarehouse(Oracle) consulting, Unix programming
    (Java, Perl, Korn Shell).
    in Belgium, England, Luxembourg and more.
    www.logidox-computing.com
    Geoffroy Braem, Jul 27, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. engineer

    Tool for perl script to awk

    engineer, Jun 14, 2004, in forum: Perl
    Replies:
    1
    Views:
    872
    Joe Smith
    Jun 14, 2004
  2. bolega
    Replies:
    6
    Views:
    2,042
  3. AyOut

    Executing awk from perl script

    AyOut, Mar 24, 2007, in forum: Perl Misc
    Replies:
    6
    Views:
    152
    Greg Bacon
    Mar 26, 2007
  4. Replies:
    5
    Views:
    724
    John W. Krahn
    Apr 2, 2007
  5. Generic Usenet Account

    Perl equivalent of simple awk script

    Generic Usenet Account, Nov 12, 2008, in forum: Perl Misc
    Replies:
    5
    Views:
    177
Loading...

Share This Page