Solved

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

Posted on 2009-07-10
8
274 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
ID: 24825893
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
ID: 24825917
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
ID: 24825951
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 84

Expert Comment

by:ozo
ID: 24826843
I don't get that error.
Are you sure you are enclosing the command in quotes?
0
 

Author Comment

by:jl66
ID: 24828011
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
ID: 24828021
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
ID: 24828135
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
ID: 24828136
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…

837 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