0

If I have document and want to iterate the second column of the document into an array, would there be a simple way to do this. At present I am trying by using:

cat file.txt | awk -F'\t' '{print $2}' | sort -u

This lists all the unique items in the second column to standard out.

The question is ...how do I now add these items to an array, considering some of these items have whitespace.

I have been trying to declare an array

arr=()

and then tried

${arr}<<cat file.txt | awk -F'\t' '{print $2}' | sort -u

SqLITE
  • 35
  • 5

2 Answers2

2

Bash4+ has mapfile aka readarray plus a Process Substituion.

mapfile -t array < <(awk -F'\t' '{print $2}' file.txt | sort -u)

If you don't have bash4+

while IFS= read -r line; do
  array+=("$line")
done < <(awk -F'\t' '{print $2}' file.txt | sort -u)

To see the structure in the array

declare -p array
  • By default read strips leading and trailing white space so to work around that you need to use IFS= to preserve the default line structure.

  • The -t option from mapfile -t Remove a trailing DELIM from each line read (default newline)

Jetchisel
  • 7,493
  • 2
  • 19
  • 18
2

Bash 3 has read -a to read IFS delimited fields from a file stream to an array. The -d '' switch tells read, the record delimiter is null, so it reads fields until it reaches the end of the file stream EOF or a null character.

declare -a my_array
IFS=$'\n' read -r -d '' -a my_array < <(cut -f2 < file.txt | sort -u)
Léa Gris
  • 17,497
  • 4
  • 32
  • 41