-1

您好,我想创建一个执行以下操作的 bash 脚本:

我有 2 个文本文件,一个是 adf.ly 链接,另一个是 Recipie 名称

我想创建一个批处理脚本,从每个文本文件中获取每一行并执行以下操作

<li><a href="**TEXT FILE 1 ADFLY URL line 1** ">**Recipie name line 1 of txt file**</a></li>

<li><a href="**TEXT FILE 1 ADFLY URL line 2** ">**Recipie name line 2 of txt file**</a></li>

ect ect 并将所有结果保存到另一个名为 LINKS.txt 的文本文件中

有人请帮助或指出我的 linux bash 脚本方向

4

2 回答 2

1

这个 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
于 2013-04-14T17:10:15.067 回答
0

尝试这样做:

$ cat file1
aaaa
bbb
ccc

$ cat file2
111
222
333

$ paste file1 file2 | while read a b; do
    printf '<li><a href="file://%s">%s</a></li>\n' "$a" "$b"
done | tee newfile

输出

<li><a href="file://aaaa">111</a></li>
<li><a href="file://bbb">222</a></li>
<li><a href="file://ccc">333</a></li>
于 2013-04-14T17:06:08.750 回答