Question

With the following Perl liner command I use to match the LAST_WORD in file

  • Last word could be with spaces or without spaces

    perl -ne "print if /LAST_WORD\s*$/" file
    

Perl command work fine on Linux and Solaris with Perl version - v5.8.7

But when I run it on Perl version 5.6.1 on Solaris 9 machine

I get the following errors

   perl -ne "print if /LAST_WORD\s*$/" file
   Illegal variable name.

Please advice what need to fix in my syntax so the Perl command will support all Perl versions ?

Or maybe other good alternative to match last word in file ( with or without spaces )

Was it helpful?

Solution 2

It has nothing to do with Perl, and everything to do with your shell.

The problem is that $ is special for your shell in double-quoted literals. You could escape it, but it's cleaner to use single quotes instead. The content of single-quoted text is passed unchanged.

perl -ne 'print if /LAST_WORD\s*$/' file

OTHER TIPS

Try with quoting:

perl -ne "print if /LAST_WORD\s*\$/" file

It's a more general solution than single quotes.

I confirm on Linux Perl 5.8+ it works also without escaping... Can't check on Solaris right now, sorry... But I suppose the problem comes from a different variable resolution within double quotes logic in the shell on Solaris with respect the one on Linux...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top