|
|
|
|
|
by scdlbx
4179 days ago
|
|
It's a lot easier to delete specific lines using sed. Also you can have sed do replacements to the n'th instance of something. Doing that in Perl is a bit more complicated and a lot less succint. $ echo "foo foo foo foo" | sed 's/foo/bar/3' foo foo bar foo |
|
$ perl6 -pe 'next if ++$ == 2' example.txt
... prints all lines except line 2.
This is an example from Perl 6 One Liners[1].
The `$` is just just an unnamed variable that is getting incremented once per evaluation (-e is for `evaluate`) which in this case happens once per line (-p is for printing each line of input after eval'ing the code -- unless a `next` applies, in which case that line gets skipped).
And...
$ echo "foo foo foo foo" | perl6 -pe 's:3rd/foo/bar/'
... replaces the third foo with bar.
P6 regexes are far easier to read and way more powerful than P5 regexes. The `:3rd` bit is a general language feature called "Adverbs", in this case applied to the regex focused s/// built in.[2]
[1] https://github.com/sillymoose/Perl6-One-Liners
[2] http://doc.perl6.org/language/regexes#Adverbs