对于写判断的语句,如for,需要放在花括号里头,{},否则会报错,这是AWK的语法:
cat sql.txt
http://adff.com 300,300,300
http://adff1.com 300,300,300
http://adff2.com 300,300,300
http://adff3.com 300,300,300
http://adff4.com 300,300,300,300,300
cat sql.txt |awk '{a=split($2,filearray,",")}{for (i=1;i<=a;i++) printf ("%s/v=%s\n",$1,i)}'
http://adff.com/v=1
http://adff.com/v=2
http://adff.com/v=3
http://adff1.com/v=1
http://adff1.com/v=2
http://adff1.com/v=3
http://adff2.com/v=1
http://adff2.com/v=2
http://adff2.com/v=3
http://adff3.com/v=1
http://adff3.com/v=2
http://adff3.com/v=3
http://adff4.com/v=1
http://adff4.com/v=2
http://adff4.com/v=3
http://adff4.com/v=4
http://adff4.com/v=5
之前加个end,发现只输出最后一行,不对。去了END就好了,end表示其后的语句在所有语句执行后才执行,做一些计算后的总结打印输出,BEGIN表示在所有语句执行前执行:
http://adff4.com/1
http://adff4.com/2
http://adff4.com/3
http://adff4.com/4
http://adff4.com/5
cat sql.txt
http://adff.com 300,300,300
http://adff1.com 300,300,300
http://adff2.com 300,300,300
http://adff3.com 300,300,300
http://adff4.com 300,300,300,300,300
cat sql.txt |awk '{a=split($2,filearray,",")}{for (i=1;i<=a;i++) printf ("%s/v=%s\n",$1,i)}'
http://adff.com/v=1
http://adff.com/v=2
http://adff.com/v=3
http://adff1.com/v=1
http://adff1.com/v=2
http://adff1.com/v=3
http://adff2.com/v=1
http://adff2.com/v=2
http://adff2.com/v=3
http://adff3.com/v=1
http://adff3.com/v=2
http://adff3.com/v=3
http://adff4.com/v=1
http://adff4.com/v=2
http://adff4.com/v=3
http://adff4.com/v=4
http://adff4.com/v=5
之前加个end,发现只输出最后一行,不对。去了END就好了,end表示其后的语句在所有语句执行后才执行,做一些计算后的总结打印输出,BEGIN表示在所有语句执行前执行:
http://adff4.com/1
http://adff4.com/2
http://adff4.com/3
http://adff4.com/4
http://adff4.com/5
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/9840/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2018-8-10 11:24
评论列表