Unix parsing pipe delimited format string in ksh

前端 未结 2 659
闹比i
闹比i 2021-01-21 22:07

I am writing ksh script to parse a pipe delimited string

export dummy=\"abc\"  
echo \"123|456|789\" | awk \'{split($0,output,\"|\"); print output[3] output[2] o         


        
相关标签:
2条回答
  • 2021-01-21 22:30

    The shell can do it:

    line="123|456|789"
    IFS='|' read a b c <<END
    $line
    END
    echo $c  # => 789
    
    0 讨论(0)
  • 2021-01-21 22:36

    You can't assign awk variables (i.e. output[3]) to shell variables (i.e. dummy), you can only assign the output of awk to a variable, e.g.

    export dummy=`echo "123|456|789" | awk -F'|' '{ print $3; }'`
    

    However, awk is a bit overkill here, cut will work just as well:

    export dummy=`echo "123|456|789" | cut -d'|' -f3`
    
    0 讨论(0)
提交回复
热议问题