--modify-files does not work with `$` in regex as searching does
Closed this issue · 4 comments
If I have this file in file.txt
(with newline at the end):
a
and I run this command
$ rak '{ "a" if .match(/a$/) }' file.txt
1:a
then I correctly get the match. But when I run the same command with --modify-files
$ rak --modify-files '{ "a" if .match(/a$/) }' file.txt
Processed 0 files
then no files are processed. This can be fixed by changing regex from /a$/
to /a$$/
and replacement to "a\n"
to save the newline:
$ rak --modify-files '{ "a\n" if .match(/a$$/) }' file.txt
Processed 1 file
The fact that searching and modifying works differently regarding $
was confusing to me, maybe it is worth fixing or clarifying.
From the documentation: "The Callable will be called for each file (in sorted order) and each line of the file, giving the line (including its line ending)".
So the .match(/a$/)
would not match, so Empty
was being returned always. So you would indeed need a /a$$/
as the pattern.
The reason the \n
is included, is that you may want to remove that to merge lines.
Now, whether that is a sensible default, I would have to think about that.
We could let it default to not including, and if you want them, specify --with-line-endings
. Hmmmm...
Oh, interesting, the documentation in rak --help=content
does not mention that. I will consult the docs at https://raku.land/zef:lizmat/App::Rak too.
I've just released 0.3.12 (which should be visible there in a few minutes). You should be able to download it now already (might need a zef --update
).
This will default to not producing line endings, specify --with-line-endings
if you do want them.
I also added a mention to the --help
page.
Works on my side, thank you!