如何基于输入的文本文档创建html链接 [英] How to create html links based on a text document of inputs

查看:55
本文介绍了如何基于输入的文本文档创建html链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以基本上我有输入文本:例如,

Alright so basically I've got a txt of inputs: e.g,

fde
fefe
4fd
f3faf

我想获取文本文档中的所有输入,并使用一个bash脚本来输出HTML链接,例如

I would like to take all of the inputs in the text document and have a bash script which outputs HTML links, e.g.

<a href='https://wwww.example.com/fde/'>https://wwww.example.com/fde/</a>
<a href='https://wwww.example.com/fefe/'>https://wwww.example.com/fefe/</a>

因此对于每个字符行文件,都希望在每行上带有超链接的html.非常感谢您的帮助.我正在尝试在bash/linux终端中执行此操作.

So for each file of lines of characters, an html with hyperlinks on each line is expected. Thank you much if you can help. I'm attempting to do this in bash/linux terminal.

推荐答案

假设文件名为 test.txt ,则可以通过以下方式解决该问题:

Assuming the file is named test.txt, you can solve it the following way:

cat test.txt | while read string; do echo "<a href='https://wwww.example.com/$string'>https://wwww.example.com/$string</a>"; done

输出为:

<a href='https://wwww.example.com/fde'>https://wwww.example.com/fde</a>
<a href='https://wwww.example.com/fefe'>https://wwww.example.com/fefe</a>
<a href='https://wwww.example.com/4fd'>https://wwww.example.com/4fd</a>
<a href='https://wwww.example.com/f3faf'>https://wwww.example.com/f3faf</a>

您还可以创建一个bash脚本来期望传递文件位置:

You can as well create a bash script that expects a file location to be passed:

#!/bin/bash

[[ -z $1 || ! -f $1 ]] && echo "File not found." && exit 1

cat $1 | \
    while read string; do
        echo "<a href='https://wwww.example.com/$string'>https://wwww.example.com/$string</a>"
    done

请注意,此解决方案不会转义字符串,即,如果您的字符串包含HTML实体,例如< > & "',您可能需要对其进行转义.

Note that this solution does not escape the strings, i.e. if your strings contain HTML entities like <, >, &, " or ', you may need to escape them.

这篇关于如何基于输入的文本文档创建html链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆