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

0

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

2 Comments

This perfectly answers the question. +1 for adding non BASh4+
Good to know, you can accept the answer if you think it solves whatever you're trying to do.
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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.