有一个文本文件,第一行是link
,第二行是username
,第三行是gallery
:
http://sitename.com/galleries/1
1-username
gallery 1
http://sitename.com/galleries/2
2-username
gallery 2
http://sitename.com/galleries/3
1-username
gallery 3
http://sitename.com/galleries/4
2-username
gallery 4
http://sitename.com/galleries/5
3-username
gallery 5
对于每个link
需要curl
它,grep
一些图片的链接,并使用wget
将其下载到名为gallery
的文件夹中名为username
的文件夹。
例如,需要有这样的文件夹结构:
\1-username\gallery 1
\2-username\gallery 2
\1-username\gallery 3
\2-username\gallery 4
\3-username\gallery 5
代码框(使用sed
输出变量的2个反引号):
| curl $link | egrep 'PATTERN' | for image in ``sed -e "s/PATTERN/\\1/"`` do wget $image -P "\\$username\\$gallery";done
如何在读取这样的文本文件时为cylce中的字符串设置不同的变量?
答案 0 :(得分:2)
使用awk
:
awk '/http/{p=NR;next} NR==p+1{u=$0;next} NR==p+2{print "\\" u "\\" $0; p=""}' file
\1-username\gallery 1
\2-username\gallery 2
\1-username\gallery 3
\2-username\gallery 4
\3-username\gallery 5
答案 1 :(得分:1)
您可以使用mod运算符和计数器来读取文件,以便获得正确的变量,例如。
C=0
while read line; do
if (( (C % 3) == 0 )); then
link=$line
elif (( (C % 3) == 1 )); then
username=$line
else
gallery=$line
#Do what you want with the varible here
echo "$link - $username - $gallery"
fi
C=$((C+1)) #Counter
done < file.txt
我只是回显变量,其输出在下面,但您可以在那里运行代码。
http://sitename.com/galleries/1 - 1-username - gallery 1
http://sitename.com/galleries/2 - 2-username - gallery 2
http://sitename.com/galleries/3 - 1-username - gallery 3
http://sitename.com/galleries/4 - 2-username - gallery 4
http://sitename.com/galleries/5 - 3-username - gallery 5