acrobat118 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I am a totally new on perl. I am trying to get data from files with some modifications. I am using the following bash script for each individual *log file.

grep "R35LD1A" HF_UHF_02.50_plot.log | awk '($2**2 <0.00001&&$3**2.<0. +0001){print $4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19 +,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30, $31,$32,$33,$34,$35,$36,$37,$38,$39,$40}' |sort -g -k1 >HF_UHF_02.50_ +plot.res

Since there are a lot of files so it is hard to do manually using bash for each file. I was trying to write a perl script but I dont know how to modify the above bash script to perl. Please help me. I shall be thankful.

Replies are listed 'Best First'.
Re: Bash script to perl script
by haukex (Archbishop) on Jun 18, 2016 at 11:15 UTC
Re: Bash script to perl script
by hippo (Archbishop) on Jun 18, 2016 at 12:19 UTC
    Since there are a lot of files so it is hard to do manually using bash for each file.

    Bash answer: use a loop.

    Perl has a steep learning curve, and I encourage you to follow the references in the other replies. Perl is much, much more flexible powerful and efficient than bash (or any other shell) in most circumstances and the time spent learning it will pay huge dividends. But, if you just want a quick solution you can look to use the tools with which you are already familiar.

    One other top tip: never* pipe grep into awk, it's a waste of a process. Instead of your grep above you can do

    awk '/R35LD1A/ && ($2**2 <0.00001&&$3**2.<0.0001){ ...

    *Well, almost never. There are exceptions to every rule after all.

Re: Bash script to perl script
by stevieb (Canon) on Jun 18, 2016 at 11:01 UTC

    Well, we'd need to see a chunk of the input file, and an example of what your expected output looks like. Put both of those in <code></code> tags like you've put your Bash code in.

    Also, no matter what it looks like, please show the Perl code that you've made an attempt with, so the Monks can see that you have made at least an attempt.

Re: Bash script to perl script
by ww (Archbishop) on Jun 18, 2016 at 11:04 UTC

    Start by studying "Learning Perl" ( Rent The Llama at O'Reilly); the Monastery's Tutorials; and/or any number of on-line college level courses.

    Then come back with questions about those constructs on which you get stuck: questions including code, some small snippet of data, and exact error/warning messages.

    Why? because this is a spot to learn; not (free) code-a-matic!


    Sorry, we expect SOPW to seek wisdom, not to ask us to do so for them.

Re: Bash script to perl script
by RonW (Parson) on Jun 24, 2016 at 23:50 UTC

    There is a2p that can convert most AWK scripts into Perl code.

    If you do as Hippo suggested, incorporating your grep pattern into AWK:

    awk '/R35LD1A/ && ($2**2 <0.00001&&$3**2.<0.0001){ ...

    then a2p will include that in the Perl code it generates.