Shell 在while循环中读取变量时Awk中断
我试图从一个名为index.db的文本文件生成一个html文件。 index.db的内容:Shell 在while循环中读取变量时Awk中断,shell,awk,sh,Shell,Awk,Sh,我试图从一个名为index.db的文本文件生成一个html文件。 index.db的内容: file="test.html" date="2013-01-07" title="Example title" file="test2.html" date="2014-02-04" title="Second example title" 我正在尝
file="test.html"
date="2013-01-07"
title="Example title"
file="test2.html"
date="2014-02-04"
title="Second example title"
我正在尝试的命令:
sed '/^$/d;H;1h;$!d;g;s/\n\t\+/ /g' input/index.db |
while read -r line; do
awk '{
print "<h1>"title"</h1>"
print "<b>"date"</b>"
print "<a href=\""file"\">"file"</a>"
}' $line
done
但如果我尝试以下命令,它将运行得非常完美:
sed '/^$/d;H;1h;$!d;g;s/\n\t\+/ /g' input/index.db |
while read -r line; do
echo $line
awk '{
print "<h1>"title"</h1>"
print "<b>"date"</b>"
print "<a href=\""file"\">"file"</a>"
}' file="test.html" date="2013-01-07" title="Example title"
done
Awk设计用于处理文件,因此您不需要在循环中逐行处理。此外,awk和sed通常是相互可变的,但很少一起使用。使用完整的awk解决方案,您可以做您需要做的事情。使用GNU awk:
awk '/file=/ { lne=gensub(/(^.*=")(.*)(\".*$)/,"<a href=\"\\2\">\\2</a>",$0);print lne} /date=/ {lne=gensub(/(^.*=")(.*)(\".*$)/,"<b>\\2</b>",$0);print lne} /title=/ {lne=gensub(/(^.*=")(.*)(\".*$)/,"<h1>\\2</h1>",$0);print lne}' input/index.db
说明:
awk '/file=/ {
lne=gensub(/(^.*=")(.*)(\".*$)/,"<a href=\"\\2\">\\2</a>",$0); # Use the gensub function to split any lines with "file", into three section, leaving the section between quotes in section 2. We then surround section 2 with the required htlm and read the result in to the variable lne.
print lne # Print lne
}
/date=/ { # Use the same logic for lines with date.
lne=gensub(/(^.*=")(.*)(\".*$)/,"<b>\\2</b>",$0);
print lne
}
/title=/ { # Use the same logic for lines with title.
lne=gensub(/(^.*=")(.*)(\".*$)/,"<h1>\\2</h1>",$0);
print lne
}' input/index.db
输出:
<a href="test.html">test.html</a>
<b>2013-01-07</b>
<h1>Example title</h1>
<a href="test2.html">test2.html</a>
<b>2014-02-04</b>
<h1>Second example title</h1
这种方法也可以以与sed非常类似的方式使用:
sed -r '/file=/s@(^.*=")(.*)(\".*$)@<a href=\"\2\">\2</a>@;/date=/s@(^.*=")(.*)(\".*$)@<b>\2</b>@;/title=/s@(^.*=")(.*)(\".*$)@<h1>\2</h1>@' input/index.db
Awk设计用于处理文件,因此您不需要在循环中逐行处理。此外,awk和sed通常是相互可变的,但很少一起使用。使用完整的awk解决方案,您可以做您需要做的事情。使用GNU awk:
awk '/file=/ { lne=gensub(/(^.*=")(.*)(\".*$)/,"<a href=\"\\2\">\\2</a>",$0);print lne} /date=/ {lne=gensub(/(^.*=")(.*)(\".*$)/,"<b>\\2</b>",$0);print lne} /title=/ {lne=gensub(/(^.*=")(.*)(\".*$)/,"<h1>\\2</h1>",$0);print lne}' input/index.db
说明:
awk '/file=/ {
lne=gensub(/(^.*=")(.*)(\".*$)/,"<a href=\"\\2\">\\2</a>",$0); # Use the gensub function to split any lines with "file", into three section, leaving the section between quotes in section 2. We then surround section 2 with the required htlm and read the result in to the variable lne.
print lne # Print lne
}
/date=/ { # Use the same logic for lines with date.
lne=gensub(/(^.*=")(.*)(\".*$)/,"<b>\\2</b>",$0);
print lne
}
/title=/ { # Use the same logic for lines with title.
lne=gensub(/(^.*=")(.*)(\".*$)/,"<h1>\\2</h1>",$0);
print lne
}' input/index.db
输出:
<a href="test.html">test.html</a>
<b>2013-01-07</b>
<h1>Example title</h1>
<a href="test2.html">test2.html</a>
<b>2014-02-04</b>
<h1>Second example title</h1
这种方法也可以以与sed非常类似的方式使用:
sed -r '/file=/s@(^.*=")(.*)(\".*$)@<a href=\"\2\">\2</a>@;/date=/s@(^.*=")(.*)(\".*$)@<b>\2</b>@;/title=/s@(^.*=")(.*)(\".*$)@<h1>\2</h1>@' input/index.db
有了你们展示的样品,你们能试一下下面的吗。这将生成一个包含标题、正文和所有标记的正确HTML文件
awk '
BEGIN{
print "<html>"ORS"<title>Your title here..</title>"ORS"<body>"
}
!NF{ val="" }
match($0,/"[^"]*/){
val=substr($0,RSTART+1,RLENGTH-1)
}
/^file=/{
print "<a href=\"" val "\"</a>"
next
}
/date=/{
print "<b>" val "</b>"
next
}
/title/{
print "<h1>"val"</h1>"
}
END{
print "</body>" ORS "</html>"
}
' Input_file
上面将根据所示的示例详细信息生成以下html文件:
<html>
<title>Your title here..</title>
<body>
<a href="test.html"</a>
<b>2013-01-07</b>
<h1>Example title</h1>
<a href="test2.html"</a>
<b>2014-02-04</b>
<h1>Second example title</h1>
</body>
</html>
有了你们展示的样品,你们能试一下下面的吗。这将生成一个包含标题、正文和所有标记的正确HTML文件
awk '
BEGIN{
print "<html>"ORS"<title>Your title here..</title>"ORS"<body>"
}
!NF{ val="" }
match($0,/"[^"]*/){
val=substr($0,RSTART+1,RLENGTH-1)
}
/^file=/{
print "<a href=\"" val "\"</a>"
next
}
/date=/{
print "<b>" val "</b>"
next
}
/title/{
print "<h1>"val"</h1>"
}
END{
print "</body>" ORS "</html>"
}
' Input_file
上面将根据所示的示例详细信息生成以下html文件:
<html>
<title>Your title here..</title>
<body>
<a href="test.html"</a>
<b>2013-01-07</b>
<h1>Example title</h1>
<a href="test2.html"</a>
<b>2014-02-04</b>
<h1>Second example title</h1>
</body>
</html>
使用一些可重用的函数来包装html标记
$ awk -F'[="]' -v RS= -v OFS='\n' -v ORS='\n\n' '
function h(t,r,v) {return "<" t (r?" href=\"" r "\"":"") ">"v "</"t">"}
{print h("h1","",$9), h("b","",$6), h("a",$3,$3)}' file
<h1>Example title</h1>
<b>2013-01-07</b>
<a href="test.html">test.html</a>
<h1>Second example title</h1>
<b>2014-02-04</b>
<a href="test2.html">test2.html</a>
使用一些可重用的函数来包装html标记
$ awk -F'[="]' -v RS= -v OFS='\n' -v ORS='\n\n' '
function h(t,r,v) {return "<" t (r?" href=\"" r "\"":"") ">"v "</"t">"}
{print h("h1","",$9), h("b","",$6), h("a",$3,$3)}' file
<h1>Example title</h1>
<b>2013-01-07</b>
<a href="test.html">test.html</a>
<h1>Second example title</h1>
<b>2014-02-04</b>
<a href="test2.html">test2.html</a>
echo$line本身就是有缺陷的,出于同样的原因,awk也是如此$线总是,总是引用你的扩展:echo$line和awk$行-看…也就是说,在循环中启动awk是一种代码气味-表明您做错了什么。几乎总是最好只运行一次awk,让它自己执行循环。@CharlesDuffy echo$line只是想看看命令是否运行了预期的次数。也就是说,注释$line不会给出错误,但仍然不会给出所需的输出。echo$line本身有缺陷,出于同样的原因,awk也是如此$线总是,总是引用你的扩展:echo$line和awk$行-看…也就是说,在循环中启动awk是一种代码气味-表明您做错了什么。几乎总是最好只运行一次awk,让它自己执行循环。@CharlesDuffy echo$line只是想看看命令是否运行了预期的次数。也就是说,注释$line不会给出错误,但仍然不会给出所需的输出。