Set variable in current shell from awk

后端 未结 7 1927
花落未央
花落未央 2020-12-29 05:40

Is there a way to set a variable in my current shell from within awk?

I\'d like to do some processing on a file and print out some data; since I\'ll rea

7条回答
  •  遥遥无期
    2020-12-29 06:06

    Here's another way.

    This is especially useful when when you've got the values of your variables in a single variable and you want split them up. For example, you have a list of values from a single row in a database that you want to create variables out of.

    val="hello|beautiful|world" # assume this string comes from a database query
    read a b c <<< $( echo ${val} | awk -F"|" '{print $1" "$2" "$3}' )
    
    echo $a #hello
    echo $b #beautiful
    echo $c #world
    

    We need the 'here string' i.e <<< in this case, because the read command does not read from a pipe and instead reads from stdin

提交回复
热议问题