Sadık
Sadık

Reputation: 4419

Store awk result in variables in bash script

I want to extract the row of a CSV file where column 4 contains a certain number.

The CSV file's rows look like this:

Markus;Haltmeyer;ID;SomeIdentifier

I want to store the first column and second column in different variables each, if SomeIdentifier is fownd.

In the bash script I only have the first characters of SomeIdentifier in a variable firstPartOfID. But nevertheless the correct row is found with the following command:

result=$(awk -v pat="${firstPartOfID}" -F ";" '$0~pat{print $1, $2 }' MyFile.csv)
echo ${result}

Unfortunately result contains both columns. I could try to split $result afterwards, but I want to do it with awk directly.

Upvotes: 2

Views: 1060

Answers (2)

hek2mgl
hek2mgl

Reputation: 157947

You can use read together with process substitution:

read var1 var2 < <(awk -v regexp="${firstPartOfID}" -F ";" '$0~regexp{print $1, $2 }')

I assume that the output does not contain whitespace (except of the delimiter). Otherwise you need to use a different output delimiter in awk and use that also in read:

IFS=";" read var1 var2 < <(awk -v regexp="${firstPartOfID}" 'BEGIN{FS=OFS=";"}$0~regexp{print $1, $2 }')

I'm using the ; as the output delimiter in the above example. It makes sense to use it because it is also the input delimiter and therefore it is guaranteed to be not contained in the data.


Btw, instead of using a regular expression you may use the index() function in awk. That would be more efficient.

awk -v id_prefix="${firstPartOfID}" -F ";" 'index($3, id_prefix){print $1, $2 }'

Upvotes: 3

Eric Renouf
Eric Renouf

Reputation: 14490

You can also do this skipping awk if you want multiple values, and just use bash to do the pattern matching:

while IFS=\; read first last idfield rest; do
    if [[ $idfield =~ $firstPartOfID ]]; then
        first_name=$first
        last_name=$last
        break
    fi
done < MyFile.csv

or depending on what you want to do with those values after, you might be able to do that within awk

Upvotes: 2

Related Questions