having issues using awk and/or converting to perl

Discussion in 'Perl Misc' started by jthrumston@gmail.com, Jan 9, 2006.

  1. Guest

    Hi, I am not a strong awk user and have little to no experience with
    perl.

    I was reading a thread about how to use awk in a perl script (cause I
    haven't figured out how to do the same in perl)

    Here is my awk command

    awk '$1 ~ /^F/ {print $0 }' /tmp/frhtest/ADDC.bak >
    /tmp/frhtest/ADDC.$date

    this works fine at the command line. I tried to do this to run inside
    my perl script.

    $call = `awk '$1 ~ /^F/ {print $0 }' /tmp/frhtest/ADDC.bak >
    /tmp/frhtest/ADDC.$date`;
    system("$call");

    and I get this result
    syntax error The source line is 1.
    The error context is
    >>> ~ <<<

    awk: Quitting
    The source line is 1.
    it generates a zero record file.

    Basically all I am doing is pulling all records out of a file that
    start with "F" and creating a new file of just those records.

    any suggestions would be greatfully taken.

    Thanks
     
    , Jan 9, 2006
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > Hi, I am not a strong awk user and have little to no experience with
    > perl.
    >
    > I was reading a thread about how to use awk in a perl script (cause I
    > haven't figured out how to do the same in perl)
    >
    > Here is my awk command
    >
    > awk '$1 ~ /^F/ {print $0 }' /tmp/frhtest/ADDC.bak >
    > /tmp/frhtest/ADDC.$date
    >
    > this works fine at the command line. I tried to do this to run inside
    > my perl script.
    >
    > $call = `awk '$1 ~ /^F/ {print $0 }' /tmp/frhtest/ADDC.bak >
    > /tmp/frhtest/ADDC.$date`;


    In Perl, back-ticks interpolate variables. Therefore, Perl's $1 and $0
    variables are being passed to your awk script, and awk is not being
    asked to look at it's own $1 and $0 variables. Backslash each of
    those $ characters.

    > system("$call");


    perldoc -q vars

    > and I get this result
    > syntax error The source line is 1.
    > The error context is
    > >>> ~ <<<

    > awk: Quitting
    > The source line is 1.
    > it generates a zero record file.
    >
    > Basically all I am doing is pulling all records out of a file that
    > start with "F" and creating a new file of just those records.


    perl -ne'print if /^F/' oldfile.txt > newfile.txt

    Paul Lalli
     
    Paul Lalli, Jan 9, 2006
    #2
    1. Advertising

  3. Xicheng Guest

    wrote:
    > Basically all I am doing is pulling all records out of a file that
    > start with "F" and creating a new file of just those records.

    If this is what you want, just forget about using awk within perl
    scripts, the perl onelier is easy as awk:

    perl -nle 'print if /^F/' infile > outfile

    you dont need to separate line into columns except that you need to
    check contents in a specific column, like:

    perl -anle 'print if $F[2] =~ /^F/' infile > outfile

    this check if column-3 begins with 'F'...

    your awk command can be written as:

    awk '/^F/' infile > outfile

    Xicheng
     
    Xicheng, Jan 9, 2006
    #3
  4. jthrumston Guest

    I tried this as you suggested and got the following error.

    Bareword found where operator expected at ./trial line 6, near "'print
    if /^F/' ADDC"
    (Missing operator before ADDC?)
    syntax error at ./trial line 6, near "-ne"
    Execution of ./trial aborted due to compilation errors.
     
    jthrumston, Jan 9, 2006
    #4
  5. Paul Lalli Guest

    jthrumston wrote:
    > I tried this as you suggested


    What is "this"? Who is "you"?

    Please quote context when posting a reply, as I have done by including
    your comments in my reply.

    > and got the following error.
    >
    > Bareword found where operator expected at ./trial line 6, near "'print
    > if /^F/' ADDC"
    > (Missing operator before ADDC?)
    > syntax error at ./trial line 6, near "-ne"
    > Execution of ./trial aborted due to compilation errors.


    The code I posted was a "one-liner", just as your original awk script
    was. It is not meant to be run inside of any other source code. Run
    that code directly at the command line.

    Paul Lalli
     
    Paul Lalli, Jan 9, 2006
    #5
  6. jthrumston Guest

    I tried your first line perl -nle 'print if /^F/' infile > outfile

    and got this

    String found where operator expected at ./trial line 6, at end of line
    (Missing semicolon on previous line?)
    Can't find string terminator "'" anywhere before EOF at ./trial line 6.


    on the second one, does that mean column 1 would be $F[0] ?
     
    jthrumston, Jan 9, 2006
    #6
  7. jthrumston Guest

    ok, so I tried that and it didn't work either. How would I incorporate
    that into a script. This is only 1 part of what my script is going to
    do, I actually have 11 files to do repeat this action with.
     
    jthrumston, Jan 9, 2006
    #7
  8. Paul Lalli Guest

    jthrumston wrote:
    > ok, so I tried that and it didn't work either. How would I incorporate
    > that into a script. This is only 1 part of what my script is going to
    > do, I actually have 11 files to do repeat this action with.


    I have asked you once already to please quote context when posting a
    reply. Your decision not to do this is not making me overly anxious to
    help you.

    Please go read the posting guidelines for this group. They are posted
    here twice a week. Take note of, in particular, effective follow-up
    style, and the request to post a short-but-complete script that
    demonstrates your error. "it didn't work" does not help in the least.

    Once you have composed a message that conforms to these Posting
    Guidelines, you are far more likely to receive help in figuring out
    your problem.

    Paul Lalli
     
    Paul Lalli, Jan 9, 2006
    #8
  9. jthrumston Guest

    I apologize for not showing more detail in my post. I will remember to
    quote more in the future
     
    jthrumston, Jan 9, 2006
    #9
  10. jthrumston Guest

    This is also my first time using the boards. I did not read the
    procedure as I should have. I will read them now to make sure my posts
    are more informative and comply with standards. Again I apologize for
    not being more clear.
    Thanks for the heads up Paul.
     
    jthrumston, Jan 9, 2006
    #10
  11. Matt Garrish Guest

    "jthrumston" <> wrote in message
    news:...
    > This is also my first time using the boards. I did not read the
    > procedure as I should have. I will read them now to make sure my posts
    > are more informative and comply with standards. Again I apologize for
    > not being more clear.
    >


    Usenet is not a "board": http://www.faqs.org/faqs/usenet/what-is/part1/

    Matt
     
    Matt Garrish, Jan 9, 2006
    #11
  12. Paul Lalli Guest

    jthrumston wrote:
    > This is also my first time using the boards.


    You are not posting to "boards". You are posting to Usenet. The
    difference is significant.

    > I did not read the
    > procedure as I should have. I will read them now to make sure my posts
    > are more informative and comply with standards. Again I apologize for
    > not being more clear.


    Courtesy of "Dr. Ruud":
    For jthrumston and everybody else with "User-Agent: G2/#.#":

    "How can I automatically quote the previous message
    when I post a reply?"
    http://groups.google.co.uk/support/bin/answer.py?answer=14213

    See also:
    http://www.safalra.com/special/googlegroupsreply/

    What's good 'netiquette' when posting to Usenet?
    http://groups.google.co.uk/support/bin/answer.py?answer=12348
    http://directory.google.com/Top/Computers/Usenet/Etiquette/

    Paul Lalli
     
    Paul Lalli, Jan 9, 2006
    #12
  13. jthrumston Guest

    I got the point already.
     
    jthrumston, Jan 9, 2006
    #13
  14. Paul Lalli Guest

    jthrumston wrote:
    > I got the point already.


    No, clearly, you haven't. Because after 5 posts, you are still not
    quoting any context in your replies. I have now reached my level of
    tolerance.

    Good luck, and good bye.

    Paul Lalli
     
    Paul Lalli, Jan 9, 2006
    #14
  15. jthrumston Guest

    I choose not to quote your continually going on about my not quoting
    you. I was looking to make sure my new post contained the information
    you requested but yet again, you choose to go on about my lack of
    proper posting.
    Thanks for the help you did provide and the links to how to post Paul.
    I am very sorry this got you upset of whatever it is your experiencing.
    I might suggest trying decaf. Maybe one day when I have more experience
    I will remember your harsh tone and try not to make a first time poster
    feel like you have succeeded in making me feel. I came here for a
    little help. You decided to turn this into another matter altogether.
    Feel better?

    For those who are reading this. I apologize. I will try and get more
    precise information into my postings. I still need to resolve the perl
    issue. Hopefully without being sidetracked.

    Good day to you sir.
     
    jthrumston, Jan 9, 2006
    #15
  16. Matt Garrish Guest

    "jthrumston" <> wrote in message
    news:...
    >
    > For those who are reading this. I apologize. I will try and get more
    > precise information into my postings. I still need to resolve the perl
    > issue. Hopefully without being sidetracked.
    >


    Good luck. I doubt you'll get any more help here after this display.

    Matt
     
    Matt Garrish, Jan 9, 2006
    #16
  17. jthrumston Guest

    Xicheng wrote:
    > wrote:
    > > Basically all I am doing is pulling all records out of a file that
    > > start with "F" and creating a new file of just those records.

    > If this is what you want, just forget about using awk within perl
    > scripts, the perl onelier is easy as awk:
    >
    > perl -nle 'print if /^F/' infile > outfile
    >
    > you dont need to separate line into columns except that you need to
    > check contents in a specific column, like:
    >
    > perl -anle 'print if $F[2] =~ /^F/' infile > outfile
    >
    > this check if column-3 begins with 'F'...


    This did exactly what I need (by changing the column to 0) from the
    command line. I thank you Xicheng for suggesting that. My question now
    would be, how do I incorporate that line inside a perl script? I have
    11 files to do this exact operation on (though the column is not always
    the same from report to report).

    >
    > your awk command can be written as:
    >
    > awk '/^F/' infile > outfile
    >
    > Xicheng
     
    jthrumston, Jan 9, 2006
    #17
  18. jthrumston Guest

    This is also my first time using these boards. I am not familiar with
    the posting guidelines, but rest assured I will read them over. Again,
    my apologies for not following protocol.
     
    jthrumston, Jan 9, 2006
    #18
  19. jthrumston <> wrote:

    > I choose not to quote



    I choose to ignore all of your future posts.


    [snip whining]


    > Feel better?



    Do you?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 9, 2006
    #19
  20. <> wrote:

    > I was reading a thread about how to use awk in a perl script (cause I
    > haven't figured out how to do the same in perl)
    >
    > Here is my awk command
    >
    > awk '$1 ~ /^F/ {print $0 }' /tmp/frhtest/ADDC.bak >



    The "a2p" (awk-to-perl) translator is part of the standard
    perl distribution. You can use it to see how to do awk stuff
    using perl instead:

    echo '$1 ~ /^F/ {print $0 }' | a2p


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 9, 2006
    #20
    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. NNTP

    sed awk or perl for this?

    NNTP, Sep 11, 2003, in forum: Perl
    Replies:
    13
    Views:
    3,490
    Alan Connor
    Sep 30, 2003
  2. gorda
    Replies:
    2
    Views:
    547
    Andrew Shitov
    Oct 21, 2003
  3. hofer
    Replies:
    11
    Views:
    2,650
  4. gorda
    Replies:
    3
    Views:
    154
    Barry Kimelman
    Oct 21, 2003
  5. martin

    help with perl and awk ...

    martin, May 3, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    109
    Rahul
    May 3, 2006
Loading...

Share This Page