I'm in need for some assistance for string manipulation with sed and regex. I tried a whole day to trial & error and look around the web to find a solution however it's way over my capabilities and maybe here are some sed/regex gurus who are willing to give me a helping hand !
With everything I gathered around the web, It seems it's rather a complicated regex and sed substitution, here we go !
What Am I trying to achieve?
I have a lot of markdown guides I want to host on a self-hosted forgejo based git markdown. However the classic markdown links are not the same as one github/forgejo...
Convert the following string:
[Some text](#Header%20Linking%20MARKDOWN.md)
Into
[Some text](#header-linking-markdown.md)
As you can see those are the following requirement:
Pattern: [Some text](#link%20to%20header.md)
Only edit what's between parentheses
Replace space (%20) with -
Everything as lowercase
Links are sometimes in nested parentheses
e.g. (look here [Some text](#link%20to%20header.md))
Do not change a line that begins with https (external links)
While everything is probably a bit complex as a whole the trickiest part is probably the nested parentheses :/
What I tried
The furthest I got was the following:
sed -Ei 's|\(([^\)]+)\)|\L&|g' test3.md #make everything between parentheses lowercase
sed -i '/https/ ! s/%20/-/g' test3.md #change every %20 occurrence to -
These sed/regx substitution are what I put together while roaming the web, but it has a lot a flaws and doesn't work with nested parentheses. Also this would change every %20 occurrence in the file.
The closest solution I found on stackoverflow looks similar but wasn't able to fit to my needs. Actually my lack of regex/sed understanding makes it impossible to adapt to my requirements.
I would appreciate any help even if a change of tool is needed, however I'm more into a learning processes, so a script or CLI alternative is very appreciated :) actually any help is appreciated :D !
basically, matching #this%20is%20LIKELY%20a%20link.md
as opposed to matching whole markdown link
lowercasing that entire match,
then on a search matching stuff that looks like that, replace the %20 with a hyphen (combined into a single sed command). this only fails when an http link falls within the same line as a markdown hyperlink
sed ':loop;/\[[^]]*\](http/! s/\(\[[^]]*\]\)\(([^)]*\)%20\([^)]*)\)/\1\2-\3/g;t loop;/\[[^]]*\](http/! s/\(\[[^]]*\]\)\(([^)]*)\)/\1\L\2/g'
example file
[Some text](#Header%20Linking%20MARKDOWN.md)
(#Should%20stay%20as%20is.md)
Text surrounding [a link](readme.md#Other%20Page). Cool
Multiple [links](#Links.md) in (%20) [a](#An%20A.md) SINGLE [line](#Lines.md)
Do [NOT](https://example.com/URL%20Should%20Be%20Untouched.html) CHANGE%20 [hyperlinks](http://example.com/No%20Touchy.html)
but it doesn't work if you have a http link and markdown link in the same line, and doesn't work with [escaped \] square brackets](#and-escaped-\)-parenthesis) in the link
# use a loop to iteratively replace the %20 with -, since doing s/%20/-/g would replace too much. we loop until it cant substitute any more
# label for looping
:loop;
# skip the following substitute command if the line contains an http link in markdown format
/\[[^]]*\](http/!
# capture each part of the link, and join it together with -
s/\(\[[^]]*\]\)\(([^)]*\)%20\([^)]*)\)/\1\2-\3/g;
# if the substitution made a change, loop again, otherwise break
t loop;
# convert all insides to the link lowercase if the line doesnt contain an http link
/\[[^]]*\](http/!
# this is outside the loop rather than in the s command above because if the link doesnt contain %20 at all then it won't convert to lowercase
s/\(\[[^]]*\]\)\(([^)]*)\)/\1\L\2/g
This is more of a general suggestion: if you use Regular Expression, use https://regex101.com. It provides syntax highlighting, explains the syntax and allows you to test your regexes.
Additionally, I think that sd is way more intuitive than sed.
Honestly, I'd be looking at doing this in any other language that has a Markdown library to parse these. You're doing this on "hard mode" with sed. There are probably already a ton of Python tools out there that do this.
I have thought of a python script and looked a bit around but couldn't find something satisfactory. Also I'm a tiny bit more versed in bash/CLI than with python... Even though that's very arguable !
I looked through the Github repo and at first glance I have no idea how this could do the job, again I probably have to dig a bit deeper and understand what this is actually doing !
As I see, you've already got an answer how to convert text to lower case. So I just tell you how to replace all occurrences of %20 with -. You need to repeat substitution until no matches found. For such iteration you need to use branching to label. Below is sed script with comments.
:subst # label
s/(\[[^]]+\]\([^)#]*#[^)]*)%20([^)]*\))/\1-\2/ # replace the first occurrence of `%20` in the URL fragment
t subst # go to the `subst` label if the substitution took place
However there are some cases when this script will fail, e. g. if there is an escaped ] character in the link text. You cannot avoid such mistakes using only simple regexps, you need a full featured markdown parser for this.
NB: global substitution s///g is not applicable here because you need to perform new substitutions in a substituted text. Both sed regexp syntaxes (basic and extended) don't support lookarounds that could solve this issue.
I've got a sed regex that should work, just writing up a breakdown of the whole command so anyone interested can follow what it does. Will post in a bit.
Okay, here's the command and a breakdown. I broke down every part of the command, not because I think you are dumb, but because reading these can be complicated and confusing. Additionally, detailed breakdowns like these have helped me in the past.
The command:
sed -ri 's|]\(#.+\)|\L&|; s|%20|-|g' /path/to/somefile
The breakdown:
sed - calls sed
-r - allows for the use of extended regular expressions
-i - edit the file given as an argument at the end of the command (note, the i flag must follow the r flag, or the extended regular expressions will not be evaluated)
Now the regex piece by piece. This command has two substitution regex to break down the goals into managable chunks.
Expression one is to convert the markdown links to lowercase. That expression is:
's|]\(#.+\)|\L&|;
The goal of this expression is to find markdown links, and to ignore https links. In your post you indicate the markdown links all start with a # symbol, so we don't have to explicitly ignore the https as much as we just have to match all links starting with #. Here's the breakdown:
' - begins the entire expression set. If you had to match the ' character in your expression you would begin the expression set with " instead of '.
s| - invoking find and replace (substitution). Note, Im using the | as a separator instead of the / for easier readability. In sed, you can use just about any separator you want in your syntax
]\(# - This is how we find the link we want to work on. In markdown, every link is preceded by ]( to indicate a closing of the link text and the opening of the actual url. In the expression, the ( is preceded by a \ because it is a special regex character. So \( tells sed to find an actual closing parentheses character. Finally the # will be the first character of the markdown links we want to convert to lowercase, as indicated by your example. The inclusion of the # insures no https links will be caught up in the processing.
.+ - this bit has two parts, . and +. These are two special regex characters. the . tells sed to find any character at all and the + tells it to find the preceding character one or more times. In the case of .+, it's telling sed to find one or more of any characters. You might think this will eat ALL of the text in the document and make it all lowercase, but it will not because of the next part of the regex.
\) - this tells sed to find a closing parentheses. Like the opening parentheses, it is a special regex character and needs to be escaped with the backslash to tell sed to find an actual closing parentheses character. This is what stops the command from converting the entire document to lowercase, because when you combine the previous bit with this bit like so .+\), you're telling sed to find one or more of any character UNTIL you find a closing parentheses.
| - This tells sed we're done looking for text to match. The next bits are about how to modify/replace that text
\L - This tells sed to convert the given text to all lowercase
& - This is the given text to modify. In this case the & is a special mertacharacter that tells sed to modify the entire pattern matched in the matching portion of the expression. So when the & is preceded by the \L, this tells sed Take everything that was matched in the pattern matching expression and convert it to lowercase.
; - this tells sed that this is the end of the first expression, and that more are coming.
So all together, what this first expression does is: Find a closing bracket followed by an opening parentheses followed by a pound/hash symbol followed by one or more of any characters until finding a closing parentheses. Then convert that entire chunk of text to lowercase. Because symbols don't have case you can just convert the entire matched pattern to lowercase. If there were specific parts that had to be kept case sensitive, then you'd have to match and modify more precisely.
The next expression is pretty easy, UNLESS any of your https links also include the string %20:
If no https links contain the %20 string, then this will do the trick:
s|%20|-|g'
s| - again opens the expression telling sed wer're looking to substitute/modify text
%20 - tells sed to find exactly the character sequence %20
| - ends the pattern matching portion of the expression
- - tells sed to replace the matched pattern with the exact character -
| - tells sed that's the end of the modification instructions
g - tells sed to do this globally throughout the document. In other words, to find all occurrances of the string %20 and replace them with the string -
' - tells sed that is the end of the expression(s) to be evaluated.
So all together, what this expression does is: Within the given document, find every occurrence of a percent symbol followed by the number two followed by the number zero and replace them with the dash character.
/path/to/somefile - tells sed what file to work on.
Part of using regex is understanding the contents of your own text, and with the information and examples given, this should work. However, if the markdown links have different formatting patterns, or as mentioned any of the https links have the %20 string in them, or other text in the document might falsely match, then you'd have to provide more information to get a more nuanced regex to match.
Edit: clarified the use of the & metacharacter.
Edit 2: clarified that the + metacharacter indicates finding the preceding character (or character set) one or more times.