Interests: Regular Expressions, Linux CLI one-liners, Scripting Languages and Vim

GitHub: https://github.com/learnbyexample

Books: https://leanpub.com/u/learnbyexample

  • 3 Posts
  • 2 Comments
Joined 2 years ago
cake
Cake day: June 20th, 2023

help-circle

  • 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