您好我想创建一个执行以下操作的bash脚本:
我有2个文本文件,一个是adf.ly链接,另一个是Recipie名称
我想创建一个批处理scrript,它从每个文本文件中获取每一行并执行以下操作
<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>
并将所有结果保存到另一个名为LINKS.txt
的文本文件中有人请帮助或指点我的linux bash脚本
的方向答案 0 :(得分:1)
这个awk one-liner将完成这项工作:
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
答案 1 :(得分: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>