3

I am trying to store the result of a pattern matched by awk to a shell array variable. Here's a simplified example of the same:

#!/bin/bash
declare -a array1=()
declare -a array2=()
READ_FILE="directory1/read_file.csv"
WRITE_FILE="directory2/results.csv"

#variable for counting array index
count1=0
count2=0
#
#
# need help with line below
# $2 below is the second set of characters which is a floating point number
awk -F 'string1_to_search' '{$array1[count1++] = $2}' $READ_FILE >> $WRITE_FILE
awk -F 'string2_to_search' '{$array2[count1++] = $2}' $READ_FILE >> $WRITE_FILE
#count++ indicates post increment of count variable

any suggestions would be helpful.

3

You can not write to Bash variables from inside awk.

What you should do instead is to make awk print out everything you want to store in the array, one item per line, and then use the Bash built-in mapfile to read it into an array:

mapfile -t array1 < <( awk -F 'string1_to_search' '{print $2}' "$READ_FILE" )

After that, your array is in $array1. You also don't need to declare it beforehand, mapfile does that for you.

More information about mapfile can be found by typing help mapfile.

Side note:
I used redirected process substitution mapfile ... < <( awk ... ) here instead of a pipe awk ... | mapfile ..., because you can not use mapfile as part of a pipeline. This is because pipelines are executed in subshells, which do not propagate their changed variables back to the parent shell, i.e. the value of myarray would be lost.

  • What if the value sent to the array is a number ? I am trying to do reduction operation on the array element but I get an error and to me it seems like the element is parsed as a string rather than a number – ggulgulia Jan 7 '18 at 20:33
  • 1
    To my knowledge Bash does not differentiate between strings and numbers in variables/arrays, everything is treated as a string. Just commands may try to interpret those strings as numbers. Could you give more detail about what you are trying to do, like the actual commands, and also double-check the content of the array for correctness? Maybe you should also ask this in a new follow-up question, where you can add a link to this one for context. If you leave me a comment here once you posted it, I'll go and have a look. – Byte Commander Jan 7 '18 at 20:38
0

For example when you want the output of

awk -f TableParser.awk table.txt

to be read as a variable in shell script, just write the following:

#!/bin/sh

mapfile -t var1 < <( awk -f TableParser.awk table.txt )
echo $var1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.