Cannot run command line scripts in perl or gawk

I originally posted this to a different forum (I am a new Perl user) and realized the error so I will ask here.

I am on a WindowsXP machine trying to run perl and gawk scripts from the command line. I have perl and gawk installed and environment set to C:\perl\bin and cannot get a script to execute from the command line in Windows. The input file is in the same directory as perl.exe. When I try gawk I also am running from cmd.exe with input file in same directory.

The message that I see follows:

"The system cannot find the file specified."

I have checked that C:\Perl\bin;C:\Perl\site\bin are in the environment PATH. I don't see gawk anywhere in there though. Maybe that's a problem. Does anyone have any ideas about what I can change to get perl and gawk to work from the cmd.exe window?

I can run the simple command in perl to check the version number with no errors - 'perl -v'. Maybe that helps.

perl script - "perl -lne 'if ($.<=4) {push @x,$_} else {$x[($.-1)%4] .=" ".(split)[2]} END {print for @x}' inputfilename"
gawk script - "gawk 'NR<5 {a[$2]=$0;next} {a[$2]=a[$2] FS $NF}END {for (i in a) print a[i]|"sort -n"}' inputfilename'
Thanks to all of you. - 10000Springs(BC)

I was thinking that you put "f19" as your input file name, but that doesn't appear to be the case as can be deduced from your post above. "f19" was just a dummy file that I had created on my system to test the script.

Good that you mentioned you are on Windows. Quoting works differently on Windows than on *nix.

The Windows equivalent of my earlier post would be this -

C:\> type f19
C:\> perl -lne "if ($.<=4) {push @x,$_} else {$x[($.-1)%4] .= \" \".(split)[2]} END {print for @x}" f19

And you'd want to substitute f19 by your input file name.

Note that the main script is enclosed within double quotes, and the inner double quotes are escaped. I don't have Windows anywhere near me right now, so the commands are untested.

HTH,
tyler_durden

Tyler_Durden - Thanks for the speedy reply. I have modified the script to use the double quotes and to escape the other double quotes.

The script does work now! Awesome! :smiley:

I did have a bit of a start when I ran it the first time and got an array that was only 4 rows long by tons of columns. Then I remembered that the "4" in the script referenced the number of rows in the input file so I changed that to 65537 so it would match my input file and voila!

I have to do some more studying on all the switches and get more familiar with perl on the command line. It is a lot like awk or sed. Very useful.

I am going to mess with the gawk script to see if it needs a bit of a tweak too.

Thanks a million - 10000Springs(BC)

---------- Post updated at 01:51 PM ---------- Previous update was at 01:09 AM ----------

Well I worked with this a bit last night to get the gawk script running. I was able to make it work but only if I left out the "sort".

From reading a bit of documentation it appears that there is no sort in gawk, it uses asort instead. Even knowing this I haven't been able to get it to fly yet and running without sorting the output gives totally unusable results.

I tried:
gawk script - gawk "NR<5 {a[$2]=$0;next} {a[$2]=a[$2] FS $NF}END {for (i in a) print a[i]|\"sort -n\"}" inputfilename

so that I have replaced the quotes with double-quotes and escaped the quotes around the 'sort -n' part after the pipe. This did not work.

The only way I could execute with on error was to leave the sort after the pipe out of the script.

I will be looking deeper into the asort function in gawk over the next day or two and will post a fix if I find one.

Does anyone have any tips for an alternate method of preserving the sort order of an input in a gawk script?

Thanks for looking. --10000Springs(BC)