我的操作系统:debian8 8。
uname -a
Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.39-1+deb8u2 (2017-03-07) x86_64 GNU/Linux这是我的基本文件。
home
help
variables
compatibility
modelines
searching
selection
markers
indenting
reformatting
folding
tags
makefiles
mapping
registers
spelling
plugins
etc我想创建一个html文件如下所示。
<a href="home.html" id="home">home</a>
<a href="help.html" id="help">help</a>
<a href="variables.html" id="variables">variables</a>
<a href="compatibility.html" id="compatibility">compatibility</a>
<a href="modelines.html" id="modelines">modelines</a>
<a href="searching.html" id="searching">searching</a>
<a href="selection.html" id="selection">selection</a>
<a href="markers.html" id="markers">markers</a>
<a href="indenting.html" id="indenting">indenting</a>
<a href="reformatting.html" id="reformatting">reformatting</a>
<a href="folding.html" id="folding">folding</a>
<a href="tags.html" id="tags">tags</a>
<a href="makefiles.html" id="makefiles">makefiles</a>
<a href="mapping.html" id="mapping">mapping</a>
<a href="registers.html" id="registers">registers</a>
<a href="spelling.html" id="spelling">spelling</a>
<a href="plugins.html" id="plugins">plugins</a>
<a href="etc.html" id="etc">etc</a>每一行都添加了href和id属性,其值分别是行内容粘贴的.html和行内容本身。
如何使用vim和plugins为所有行快速添加html属性和值?
sed,awk,崇高的文本3都是欢迎解决问题的。
发布于 2017-03-28 09:17:41
如果您确信内容,sed是最好的解决方案(这里简单且速度相当快),如果不确定,则需要使用awk更好地处理一些复杂的内容:
awk '
{
# change special char for HTML constraint
Org = URL = HTML = $0
# sample of modification
gsub( / /, "%20", URL)
gsub( /</, "%3C", HTML)
printf( "<a href=\"%s\" id=\"%s\">%s</a>\n", URL, Org, HTML)
}
' YourFile发布于 2017-03-28 04:11:00
$ sed 's:.*:<a href="&.html" id="&">&</a>:' file
<a href="home.html" id="home">home</a>
<a href="help.html" id="help">help</a>
<a href="variables.html" id="variables">variables</a>
<a href="compatibility.html" id="compatibility">compatibility</a>
<a href="modelines.html" id="modelines">modelines</a>
<a href="searching.html" id="searching">searching</a>
<a href="selection.html" id="selection">selection</a>
<a href="markers.html" id="markers">markers</a>
<a href="indenting.html" id="indenting">indenting</a>
<a href="reformatting.html" id="reformatting">reformatting</a>
<a href="folding.html" id="folding">folding</a>
<a href="tags.html" id="tags">tags</a>
<a href="makefiles.html" id="makefiles">makefiles</a>
<a href="mapping.html" id="mapping">mapping</a>
<a href="registers.html" id="registers">registers</a>
<a href="spelling.html" id="spelling">spelling</a>
<a href="plugins.html" id="plugins">plugins</a>
<a href="etc.html" id="etc">etc</a>发布于 2017-03-28 16:50:18
如果您想在vi本身中执行此操作,则不需要插入。
打开文件,键入:并将该行作为命令插入
%s:.*:<a href="&.html" id="&">&</a>它将使文件中的所有替换。
https://stackoverflow.com/questions/43024359
复制相似问题