Wordzilla Wordzilla - 4 months ago 23
Perl Question

Perl autosplit inside quotes

I tried to print my apache2 access log dates with the following one liner. Within single quotes it shows the value but within double quotes it shows as Array object. My command is as follows

sudo perl -ane 'print $F[3] . "\n" ' /var/log/apache2/access.log | head -n 10


Output

[21/Jul/2014:09:16:05
[21/Jul/2014:09:16:05
[21/Jul/2014:11:32:55
[21/Jul/2014:11:32:55
[21/Jul/2014:11:32:59
[21/Jul/2014:11:33:02
[21/Jul/2014:11:33:02
[21/Jul/2014:11:33:10
[21/Jul/2014:11:33:14
[21/Jul/2014:11:33:14

sudo perl -ane "print $F[3] . \"\n\" " /var/log/apache2/access.log | head -n 10


output

ARRAY(0x195fca8)
ARRAY(0x1960128)
ARRAY(0x195fca8)
ARRAY(0x195fd38)
ARRAY(0x1960128)
ARRAY(0x195fca8)
ARRAY(0x195fd38)
ARRAY(0x1960128)
ARRAY(0x195fca8)
ARRAY(0x195fd38)


When I try to dereference the array it shows one.

sudo perl -ane "print @{$F[3]} . \"\n\" " /var/log/apache2/access.log | head -n 10


output

1
1
1
1
1
1
1
1
1
1


I am using ubuntu 12.04 64 bit with guake terminal.

Answer

When you're using double quotes $ gets interpolated by Bash and not by Perl. That's why this is happening.

Example:

a='foo bar'; perl -le "print \"$a\""
foo bar
Comments