Append .html extension to the end of all links in multiple files (with Shell Script)

1

Hello.

I have a lot of HTML files that have links in the following format: http://localhost:8080/tag:alguma_coisa and I need to add the .html extension to these links so they look like this: http://localhost:8080/tag:alguma_coisa.html .

I've tried several combinations with find and sed but with none of them I got the expected result.

Does anyone have any idea how to do this with Shell Script?

    
asked by anonymous 13.05.2018 / 19:10

2 answers

0

With the help of Marcelodez in the topic # / p>

# Ajusta os links para arquivos tag:* colocando .html ao final
# Com a importante ajudade de Marcelodez no tópico https://www.vivaolinux.com.br/topico/Sed-Awk-ER-Manipulacao-de-Textos-Strings/Acrescentar-extensao-html-ao-final-de-todos-os-links-em-varios-arquivos-com-Shell-Script/
find $OUTPUTDIR -type f -name "*.html" | xargs sed -i 's/href/\nhref/g' # quebra todos as tag A para que href comece uma nova linha
find $OUTPUTDIR -type f -name "*.html" | xargs sed -i '/^href=".*tag:.*[^#]/ s/"/.html"/2' # coloca .html ao final do valor de href para links com padrão tag:* mas que não tem #
find $OUTPUTDIR -type f -name "*.html" | xargs sed -i '/^href=".*tag:.*#/ s/#/.html#/1' # coloca .html antes do # para links no padrão tag:* com #
find $OUTPUTDIR -type f -name "*.html" | xargs sed -i '/<a/ N;s/\n//' # junta href nba mesma linha de tag
    
16.05.2018 / 13:14
1

Considering files in the current directory in .html format and within those files there are only links, it would look like this:

find . -type f -name *.html -exec sed 's/$/.html/g' {} \;
    
14.05.2018 / 16:33