1

I have a loop in a bash script that processes a number of files in a folder. In my loop, for each file, I execute the below line:

awk -F":" '{ print "Name: " $1 "\t\tID:" $3 }' $file

I would like to collect the output of all the files processed in some bash variable/array for other processing outside the loop. How can I achieve this in bash?

  • Note that processing inside a loop is often unnecessary and performance-wise worse in bash. – phk Oct 30 '16 at 8:28
2

This can be easily done using shell script array:

i=0
for file in file1 file2 ....; do
    output[$i]=$(awk -F":" '{ print "Name: " $1 "\t\tID:" $3 }' "${file}")
    i=$((${i}+1))
done
  • You should quote that variable in the awk command to protect against filenames with a space/special characters in them like so: "${file}" – Centimane Oct 14 '16 at 13:55
1

In a similar sense to a0f3dd13 you could use associative arrays. They could be useful here especially when it comes time to retrieve the data.

Associative arrays can use a string for their index rather than an integer (they're basically hash maps), so you can more easily retrieve the data.

declare -A output
for file in file1 file2 ....;do
    output["$file"]=$(awk -F":" '{ print "Name: " $1 "\t\tID:" $3 }' "${file}")
done

Then to retrieve data you can use:

echo "${output[file1]}"

This would output the results you got from file1

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.