7

I've used web search, found similarly titled question How many substitutions took place in a Perl s///g? and tried to use it to print the number but have not been able to succeed.

My initial code was

perl -0777 -i.original -pe 's-\r\n-\n-igs' test.txt

When I tried

perl -0777 -i.original -pe "$c=s-\r\n-\n-igs;say qq'$c'" test.txt

I got nothing - no output and no replacements, when I tried

perl -0777 -i.original -pe '$c=s-\r\n-\n-igs;print qq($c\n)' test.txt

(print similar to other one-liners I used before) I got empty string in standard output but 454847 added as the beginning of file (and proper replacements).

I understand =~ is not needed in my case (What does =~ do in Perl?), so what is wrong with my code? How to print number of replacements made?

zdim
  • 64,580
  • 5
  • 52
  • 81
Alex Martian
  • 3,423
  • 7
  • 36
  • 71
  • @ikegami, archname='x86_64-msys-thread-multi'. It's Git bash on Win7. I do some replacements and now want to see what many were made same as text editor informs after replace command. – Alex Martian Aug 08 '19 at 12:07

1 Answers1

9

Because of -i, the default output handle isn't STDOUT but the output file. To print to STDOUT, you'll need to do so explicitly.

If using the cmd shell,

perl -0777pe"CORE::say STDOUT s/\r//g" -i.original test.txt

If using sh or similar,

perl -0777pe'CORE::say STDOUT s/\r//g' -i.original test.txt

Notes:

  • s/// is more idiomatic than s---
  • /i is useless here.
  • /s is useless here.
  • There no point in checking for Line Feeds. s/\r//g will work just as fine as s/\r\n/\n/g.
  • There's point in using a variable for the count, especially if using say instead of print.
  • One must use CORE::say instead of say for backwards compatibility reasons unless use feature qw( say ); or equivalent is used.
  • Neither s/\r\n/\n/g nor s/\r//g will work with a Windows build of Perl, and there's no way to do what you want when using -i on a Windows build of Perl. However, you are using a unix build of Perl (since MSYS is a unix emulation environment), so it's not an issue.
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • I would add to my knowledge here found on the web: `The say function behaves just like the print function, except that it appends a newline at the end of its arguments.` and just fixed code is `perl -0777 -i.original -pe '$c=s-\r\n-\n-igs;CORE::print STDOUT qq($c\n)' test.txt `. p.s. I originally used ` as separator but stackoverflow use it to denote code. O my I did not guess that number added to the file was my output! – Alex Martian Aug 08 '19 at 12:58
  • No need to use `CORE::print` instead of `print`. `say` was added to Perl later, so one must use `CORE::say` for backwards compatibility, unless one uses `use feature qw( say );` or similar. Added this to my answer. – ikegami Aug 08 '19 at 12:59
  • I'm confused here. Why you wrote `CORE` in your answer? – Alex Martian Aug 08 '19 at 13:02
  • That was explained in the previous comment and in my answer. – ikegami Aug 08 '19 at 13:02