Arrays 循环的bash shell输出格式问题
我期望的产出是:Arrays 循环的bash shell输出格式问题,arrays,shell,awk,sed,do-while,Arrays,Shell,Awk,Sed,Do While,我期望的产出是: cat file panny daqiu 100 panny daqiu hundred yunhui heshuiREF youyong=3,hejiu=5,daren=4 yunhui heshui youyong=3,hejiu=5,daren=4 #compare row 2 with row 3, we found $2 in row 2 has one more character "REF" than $2 in row 3. 这是我的密码: pan
cat file
panny daqiu 100
panny daqiu hundred
yunhui heshuiREF youyong=3,hejiu=5,daren=4
yunhui heshui youyong=3,hejiu=5,daren=4 #compare row 2 with row 3, we found $2 in row 2 has one more character "REF" than $2 in row 3.
这是我的密码:
panny daqiu xxx #When $3 of this row only contains digits, the output of $1 & $2 will not change.
panny daqiu.hundred xxx #When $3 of this row is not a digit && $2 not caontain REF$, the output of $2 will like $2.$3
yunhui heshuiREF xxx #when $2 of this row contains character "REF"$ ,the output of $1 & $2 will not change.
yunhui heshui.youyong xxx #when $2 of this row has no character "REF"$ , and $3 like the format "A=23,B=22,C=34...", the output of $1 & $2 will be like "$1 $2.A\n $1 $2.B\n $1 $2.C\n..."
yunhui heshui.hejiu xxx
yunhui heshui.daren xxx
#"xxx" means don't need to care about output format of this column.
#Only need to care about the output format for $1 $2.
Belwo输出:
awk '{ printf("%s %s%s%s\n",$1,$2,($3!~/[[:digit:]]/||$2!~/REF$/? ".":" "),$3) }' file
那么,对于最后一行,我如何改进我的代码呢?一种方法可以做到这一点
panny daqiu 100
panny daqiu.hundred
yunhui heshuiREF youyong=3,hejiu=5,daren=4
yunhui heshui youyong=3,hejiu=5,daren=4 #I know the last row will not achieve my purpose, may be I need a loop and array[], but I am sorry for I am lack of the experience to achieve it.
真的很好,我会去了解split函数的用法。@huangcheng Cool如果您对该命令有任何疑问,请告诉我:)
awk '$2~/REF$/{print $1,$2;next}\
{gsub(/=[^,]*/,"",$3);split($3,a,",");\
for(i in a)print $1,$2(a[i]~/[[:digit:]]/?" ":".")a[i]}' file
panny daqiu 100
panny daqiu.hundred
yunhui heshuiREF
yunhui heshui.youyong
yunhui heshui.hejiu
yunhui heshui.daren