Solved

why the perl one-liner not run with variable input files

Posted on 2009-07-10
8
269 Views
Last Modified: 2012-05-07
Have the following code block
set WKDIR=D:\TEMP\
set _out1=%WKDIR%\test2.txt
set _in1=%WKDIR%\test1.txt
perl -lne "$lines{ $_ }++; END{ print for sort keys %lines }"  %_in1% > %_out1%
I got the error:
----
Missing right curly or square bracket at -e line 1, at end of line
syntax error at -e line 1, at EOF
Execution of -e aborted due to compilation errors.
----------------
but when I did the following
----------------
d:
cd D:\TEMP
perl -lne "$lines{ $_ }++; END{ print for sort keys %lines }"  test1.txt > test2.txt
----------------
I can get the right result without errors.
-------------
D:\TEMP>type test1.txt
WWW
AB_C
ABB
ACB
ABB
XYZ
123
-------
0
Comment
Question by:jl66
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 120 total points
Comment Utility
I don't know perl but could this be due to using %lines and then not ending it with another % so it doesn't terminate the variable name?

Steve
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 120 total points
Comment Utility
Probably not that.... but actually you have an extra \ in there.... one on the end of wkdir and one on beginning of other two...
0
 
LVL 11

Assisted Solution

by:willettmeister
willettmeister earned 50 total points
Comment Utility
actually you may not have enough remember \ is a special character in perl try this
set WKDIR=D:/TEMP
set _out1=%WKDIR%/test2.txt
set _in1=%WKDIR%/test1.txt

or this
set WKDIR=D:\\TEMP
set _out1=%WKDIR%\\test2.txt
set _in1=%WKDIR%\\test1.txt
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
I don't get that error.
Are you sure you are enclosing the command in quotes?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:jl66
Comment Utility
willettmeister, I tried your suggestions. I got the error messages. Also I rtied to run the same perl on different servers, I got the same. What is wrong with that.
Why don't the other one-liners have this kind of issues? ozo, I copied/pasted the code here. The quotes are here.
 
0
 

Author Comment

by:jl66
Comment Utility
dragon-it, thanks for pointing out this, but it does not make any difference.
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 170 total points
Comment Utility
The problem is the % in %lines in a batch file.  The % is a special character to batch files, so it gets interpreted along with the closing } and closing ".  This causes the syntax error.  To fix, you need to use two percent signs where you need one.
perl -lne "$lines{ $_ }++; END{ print for sort keys %%lines }"  %_in1% > %_out1%

Open in new window

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 160 total points
Comment Utility
Is this running from a .bat file?
What if you try


set WKDIR=D:\TEMP\
set _out1=%WKDIR%\test2.txt
set _in1=%WKDIR%\test1.txt
set lines=%lines
perl -lne "$lines{ $_ }++; END{ print for sort keys %lines% }"  %_in1% > %_out1%


or
perl -lne "$lines{ $_ }++; END{ print for sort keys %%lines }"  %_in1% > %_out1%
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now