请问有人解释下面脚本的含义吗?
awk -F "," 's != $1 || NR ==1{s=$1;if(p){print p};p=$0;next}
{sub($1,"",$0);p=p""$0;}
END{print p}' file
该文件包含以下数据:
2,"URL","website","aaa.com"
2,"abc","text","some text"
2,"Password","password","12345678"
3,"URL","website","10.0.10.75"
3,"uname","text","some text"
3,"Password","password","password"
3,"ulang","text","some text"
4,"URL","website","192.168.2.110"
4,"Login","login","admin"
4,"Password","password","blah-blah"
,输出为:
2,"URL","website","aaa.com","abc","text","some text",Password","password","12345678"
3,"URL","website","10.0.10.75","uname","text","some text""Password","password","password","ulang","text","some text"
答案 0 :(得分:1)
awk有这种结构
pattern {action}
对于你的脚本,让我们分析元素,第一个模式
s != $1 || NR == 1 # if the variable s is not equal to first field
# or we're operating on first row
第一个动作
s = $1 # assign variable s to first field
if (p) { # if p is not empty, print
print p
}
p = $0 # assign the current line to p variable
next # move to next, skip the rest
缺少下一个模式,因此该操作将应用于所有行
sub($1, "", $0) # same as $1="", will clear the first field
p = ((p "") $0) # concat new line to p
最后一个模式是特殊保留字END,仅在消耗所有行时应用(在文件打开之前有应用的对应BEGIN)
END {
print p # print the value of p
}