Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Simple Perl one-liner in command line

by wewantmoore (Initiate)
on Mar 25, 2015 at 11:47 UTC ( [id://1121288]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Perl users,

I am very new to Perl, and am only using it in a script together with windows command line code in order to search, compare and print from and to a file.

What I am trying to do is search for a string in an output file, compare the value behind the string to a given value, and depending on the comparison, to print a 1, or not, in another file.

The one-liner(s) I am using now are as follows:

perl -ne "if (/Variable (.*)/ > 0.900) { print 1 }" "C:\Users\Desktop\ +...\output_files\output.txt" > discard\variable.txt perl -ne "if (/Variable2 (.*)/ > 0.900) { print 1 }" "C:\Users\Desktop +\...\output_files\output.txt" > discard\variable2.txt perl -ne "if (/Variable3 (.*)/ > 0.900) { print 1 }" "C:\Users\Desktop +\...\output_files\output.txt" > discard\variable3.txt

For some reason it just doesn't work. The script will print a 1, or not, but the comparison is not correct. It also seems as if after the script is run once, the script will always print the same after that.

I'm sure (and hoping) that there is a very simple solution to my problem. Possibly just the syntax for the comparison?

Thanks in advance, Reuben.

Replies are listed 'Best First'.
Re: Simple Perl one-liner in command line
by Discipulus (Canon) on Mar 25, 2015 at 12:18 UTC
    Hi wewantmoore, and welcome.
    If i understand your question you need something like:
    perl -e "if ($ARGV[0] =~ /Variable\s(\d+)$/ and $1 > 0.9 ){print qq(Gr +eater than 0.9\n)}" "Variable 1" Greater than 0.9
    In fact inside the if clause you have something like: if the implicit $_ match this (which return value is scalar id est 1 ) is bigger than 0.9.

    I think is not what you intended.
    Regexes return many things in special variables like $1 i used. It is the content of the 1st set of parens inside a regex.
    You'll probably also need some anchor after your capturing parens because you are matching .* i used $ to specify the end of the string.
    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Simple Perl one-liner in command line
by Anonymous Monk on Mar 25, 2015 at 12:06 UTC
    perl -ne "if (/Variable3 (.*)/ > 0.900) { print 1 }" ...

    This puts the regular expression into scalar context, which means it will only return a true value on a match or a false value when not matched. $1 is populated with the contents of the first capture group, so you could write something like if (/Variable3 (.*)/ && $1 > 0.900) .... Alternatively, you can put the regular expression into list context, and since it contains capture groups and doesn't use the /g modifier it will return the contents of the capture groups as a list (details in perlop). The following puts the regex into list context and immediately fetches the first item of that list:

    perl -ne "if ( (/Variable3 (.*)/)[0] > 0.900 ) { print 1 }" ...

    Note that one debugging technique for troubles with one-liners is to put the code in a normal script file first, spell it out a little more verbosely, Use strict and warnings, find the problem (Basic debugging checklist), and then, if you still want to, reduce it back to a one-liner.

      Wow, thank you guys so much for the quick response. Your simple line of code fixed the problem and it works now.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1121288]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-23 22:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found