#!/bin/bash
# Read the data into the array fields from the
# file called "file". This assumes that the data
# is a single line with fields delimited by
# |-characters.
mapfile -d '|' -t fields <file
# The last entry in the fields array will quite
# likely have a trailing newline at the end of it.
# Remove that newline if it exists.
fields[-1]=${fields[-1]%$'\n'}
# These are the fieldcolumn numbers from the original
# data that we'd like to keep.
set -- 2 3 4
# Extract those fieldscolumns from the fields array. Note
# that we have to decrement the numbers by one as
# arrays in bash are indexed from zero.
for jcolumn do
array+=( "${fields[jfields[column-1]}" )
done
# Output the elements of the resulting array:
printf '%s\n' "${array[@]}"
# For outputting with spaces in-between the values
# rather than newlines:
printf '%s\n' "${array[*]}"
There's no reason to output the values of the array at the end if you are going to use them for other things. Saying that you want to output the values in a particular format Saying that you want to output the values in a particular format (with spaces between them) implies that you are just going to parse them again with some other codeimplies that you are just going to parse them again with some other code. That would be totally unnecessary as you already have the values in an array.
To loop over the array of values:
for value in "${array[@]}"; do
# Use "$value" here.
done
Or, using the original data array fields:
for column in 2 3 4; do
value=${fields[column-1]}
# Use "$value" here.
done