learnbyexample

joined 2 years ago
65
Introduction to Nix & NixOS (nixos-and-flakes.thiscute.world)
30
Mastering jq (codefaster.substack.com)
[–] learnbyexample@programming.dev 2 points 4 months ago

Why would it print the colon?

[–] learnbyexample@programming.dev 6 points 4 months ago (1 children)

Regex syntax and features vary between implementations. \d isn't supported by BRE/ERE flavors.

GNU grep supports PCRE, so you can use grep -oP '/dev/loop\d' or grep -o '/dev/loop[0-9]' if you are matching only one digit character.

[–] learnbyexample@programming.dev 1 points 6 months ago

Thanks a lot for the feedback :)

[–] learnbyexample@programming.dev 1 points 6 months ago

Already done grep, sed, coreutils, cli basics and more. See https://learnbyexample.github.io/learn_gnuawk/buy.html#book-list for links.

[–] learnbyexample@programming.dev 1 points 8 months ago

Well, I'm not going to even try understanding the various features used in that sed command. I do know how to use basic loops with labels, but I never bothered with all the buffer manipulation stuff. I'd rather use awk/perl/python for those cases.

[–] learnbyexample@programming.dev 2 points 8 months ago (2 children)

This might work, but I think it is best to not tinker further if you already have a working script (especially one that you understand and can modify further if needed).

perl -pe 's/\[[^]]+\]\((?!https?)[^#]*#\K[^)]+(?=\))/lc $&=~s:%20|\d\K\.(?=\d):-:gr/ge'
[–] learnbyexample@programming.dev 1 points 9 months ago (1 children)

Hmm, OP mentioned "Only edit what’s between parentheses" - don't see anywhere that whole URL shouldn't be changed...

[–] learnbyexample@programming.dev 2 points 9 months ago* (last edited 9 months ago) (7 children)

Here's a solution with perl (assuming you don't want to change http/https after the start of ( instead of start of a line):

perl -pe 's/\[[^]]+\]\(\K(?!https?)[^)]+(?=\))/lc $&=~s|%20|-|gr/ge' ip.txt
  • e flag allows you to use Perl code in the substitution portion.
  • \[[^]]+\]\(\K match square brackets and use \K to mark the start of matching portion (text before that won't be part of $&)
  • (?!https?) don't match if http or https is found
  • [^)]+(?=\)) match non ) characters and assert that ) is present after those characters
  • $&=~s|%20|-|gr change %20 to - for the matching portion found, the r flag is used to return the modified string instead of change $& itself
  • lc is a function to change text to lowercase
[–] learnbyexample@programming.dev 7 points 9 months ago (1 children)
[–] learnbyexample@programming.dev 2 points 9 months ago

Check out my chapter on GNU grep BRE/ERE for those wanting to learn this regex flavor: https://learnbyexample.github.io/learn_gnugrep_ripgrep/breere-regular-expressions.html (there's also another chapter for PCRE)

[–] learnbyexample@programming.dev 3 points 10 months ago

I use Vim ;)

Python itself provides IDLE, which is good enough for beginners. https://thonny.org/ is another good one for beginners.

As mentioned by others, Jetbrains is good for many languages. https://www.kdevelop.org/ is another option.

view more: next ›