2017-12-08 10 views
1
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. 

Welche Ausgang erwarte ich:bash-Shell-Ausgabeformat Ausgabe für Schleife

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. 

Hier ist mein Code:

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

Belwo ausgegeben:

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. 

So Wie kann ich meinen Code für die letzte Zeile verbessern?

Antwort

1

Eine Möglichkeit könnten Sie tun es

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 
+1

wirklich toll, werde ich die Verwendung von Split-Funktion zu wissen, gehen. –

+0

@huangcheng Cool, wenn Sie Fragen zum Befehl haben, lassen Sie es mich wissen :) – 123