Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set multiple variables from one awk command?

Tags:

bash

awk

This is a very common script:

#!/bin/bash

teststr="col1 col2"

var1=`echo ${teststr} | awk '{print $1}'`
var2=`echo ${teststr} | awk '{print $2}'`

echo var1=${var1}
echo var2=${var2}

However I dont like this, especially when there are more fields to parse. I guess there should be a better way like:

(var1,var2)=`echo ${teststr} | awk '{print $1 $2}' (in my imagination)

Is that so? Thanks for help to improve effeciency and save some CPU power.

like image 566
X.M. Avatar asked Mar 02 '12 07:03

X.M.


Video Answer


1 Answers

This might work for you:

var=(col0 col1 col2)
echo "${var[1]}"
col1
like image 54
potong Avatar answered Nov 03 '22 01:11

potong