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.

What output I expect:

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.

Here is my code:

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

Belwo is output:

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, for the last row, how can I improve my code ?

1 Answer 1

1

One way you could do it

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
Sign up to request clarification or add additional context in comments.

2 Comments

really great ,I will go to know the usage of split function.
@huangcheng Cool if you have any questions about the command let me know :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.