Solved

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

Posted on 2009-07-10
8
273 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

777 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