2

I have a Perl one-liner which will read a file and remove the first field of each line, which are separated by comma and dump the rest.

perl -wan -e 'for (@F) { if (/(aaa),(.*)/) {$text = $2; $text =~ s/$1// ; print "$text\n";}}'

Where aaa is in the first field of each line. This is working fine in Linux, but in Windows it is throwing an error:

Can't find string terminator "'" anywhere before EOF at -e line 1.`

Why is it behaving differently?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kumarprd
  • 906
  • 2
  • 8
  • 21
  • 1
    Possible duplicate of *[Why doesn't my Perl one-liner work on Windows?](https://stackoverflow.com/questions/660624/why-doesnt-my-perl-one-liner-work-on-windows)* – Peter Mortensen Jul 12 '19 at 19:02

2 Answers2

3

You might want to use double instead of single quotes. This might impose problems for quoted text "$text\n" which can be replaced by perl alternative quoting qq{$text\n}

perl -wane "for (@F) { if (/(aaa),(.*)/) {$text = $2; $text =~ s/$1//; print qq{$text\n}; }}"
mpapec
  • 50,217
  • 8
  • 67
  • 127
  • Obviously perl has many ways to do things. But in a perl programme, if I am using "$text", its working fine, but not in the oneliner. Is there any specific reason ?? – kumarprd Jun 19 '14 at 05:57
  • @PDK yes, double qoutes are used to enclose perl script, so it can not be used inside script unless backslashed. http://stackoverflow.com/a/660720/223226 – mpapec Jun 19 '14 at 07:45
2

In MS Windows, single quotes don't work as in Linux. You have to switch to double quotes.

perl -wan -e "for (@F) { if (/(aaa),(.*)/) {$text = $2; $text =~ s/$1// ; print qq($text\n);}}"

I used qq for the nested double quotes.

choroba
  • 231,213
  • 25
  • 204
  • 289