【问题标题】:bash-shell output format issue for loop循环的 bash-shell 输出格式问题
【发布时间】:2018-05-22 09:07:07
【问题描述】:
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.

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

【问题讨论】:

    标签: arrays shell awk sed do-while


    【解决方案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
    

    【讨论】:

    • 真的很棒,我去了解一下split函数的用法。
    • @huangcheng Cool 如果您对命令有任何疑问,请告诉我:)
    猜你喜欢
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多