循环的bash-shell输出格式问题

时间:2017-12-08 06:33:44

标签: 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.

我期望输出什么:

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.

这是我的代码:

awk '{ printf("%s %s%s%s\n",$1,$2,($3!~/[[:digit:]]/||$2!~/REF$/? ".":" "),$3) }' file

Belwo输出:

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.

那么,对于最后一行,我该如何改进我的代码?

1 个答案:

答案 0 :(得分:1)

你可以做到的一种方式

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