这个 awk 单线器将完成这项工作:
awk 'BEGIN{l="<li><a href=\"%s\">%s</a></li>\n"}NR==FNR{a[NR]=$0;next}{printf l, a[FNR],$0}' file1 file2
更清晰的版本(相同的脚本):
awk 'BEGIN{l="<li><a href=\"%s\">%s</a></li>\n"}
NR==FNR{a[NR]=$0;next}
{printf l, a[FNR],$0}' file1 file2
例子:
kent$ seq -f"%g from file1" 7 >file1
kent$ seq -f"%g from file2" 7 >file2
kent$ head file1 file2
==> file1 <==
1 from file1
2 from file1
3 from file1
4 from file1
5 from file1
6 from file1
7 from file1
==> file2 <==
1 from file2
2 from file2
3 from file2
4 from file2
5 from file2
6 from file2
7 from file2
kent$ awk 'BEGIN{l="<li><a href=\"%s\">%s</a></li>\n"};NR==FNR{a[NR]=$0;next}{printf l, a[FNR],$0}' file1 file2
<li><a href="1 from file1">1 from file2</a></li>
<li><a href="2 from file1">2 from file2</a></li>
<li><a href="3 from file1">3 from file2</a></li>
<li><a href="4 from file1">4 from file2</a></li>
<li><a href="5 from file1">5 from file2</a></li>
<li><a href="6 from file1">6 from file2</a></li>
<li><a href="7 from file1">7 from file2</a></li>
编辑 OP的评论:
如果您只有一个文件:(foo
这里只是虚拟文本)
awk 'BEGIN{l="<li><a href=\"%s\">foo</a></li>\n"}{printf l,$0}' file1
来自同一 file1 示例的输出:
<li><a href="1 from file1">foo</a></li>
<li><a href="2 from file1">foo</a></li>
<li><a href="3 from file1">foo</a></li>
<li><a href="4 from file1">foo</a></li>
<li><a href="5 from file1">foo</a></li>
<li><a href="6 from file1">foo</a></li>
<li><a href="7 from file1">foo</a></li>
如果要将输出保存到文件中:
awk 'BEGIN{l="<li><a href=\"%s\">foo</a></li>\n"}{printf l,$0}' file1 > newfile