How to replace characters from uppercase to lowercase using bash, considering those characters are inside a <a href> tag?

1

I have a lot of files with mixed case URLs that I need do convert to lowercase, changing only the links inside the files and leaving the rest of the contents intact. I found out that this is possible by using regular expressions in Notepad++:

Find in Files

Search: (href=['"])(.+)(['"])

Replace to: \1\L\2\3

However, I need to do this using bash or vim. How could I do it? Also, how to apply it to multiple files in various folders?

Fabio Romeo

Posted 2016-09-15T14:46:04.453

Reputation: 13

Answers

2

In Bash:

sed -i 's/<a href.*\">/\L&/' file.txt

In VIM:

:%s/<a href.*\">/\L&/

I wasn't sure if you just wanted the FQDN changed, or the entire URL, or if each anchor was on a line of it's own, these should cover any situation. This will ONLY change the anchored URL and not the anchor text (I am assuming that is what you are asking)

Edit:

If you want to apply this to multiple files in different folders, cd to the base directory that has all the files and subfolders with files that you want to edit, and run the following command:

find . -type f -name "*.html" | while read line; do sed -i 's/<a href.*\">/\L&/' $line; done

This will look for any files in your current directory (the one you should have cded to) and subdirectories that end with .html and will execute the aforementioned sed command. (adjust the file extension as you see fit).

Chip Shadd

Posted 2016-09-15T14:46:04.453

Reputation: 150

Your assumption is correct, I need to change only the anchored URL and not the text. It works perfectly! Just one more question (I just saw the original question and forgot it), how could I use it to a bunch of files and files in subfolders? – Fabio Romeo – 2016-09-15T19:00:02.987

@FabioRomeo I have edited my answer to accomodate your additional request. – Chip Shadd – 2016-09-16T02:27:59.823