Arrays In Bash

Read in an Array

Given a list of countries, each on a new line, your task is to read them into an array and then display the entire array, with a space between each of the countries' names.

Method 1

countries=($(cat))

Method 2

#!/bin/bash

array=()

while read line; do
    array+=("$line")
done

echo "${array[@]}"

Method 3

while read country 
 do 
   countries=( ${countries[@]} $country )
done 

echo ${countries[@]}

Method 4

declare -a arr=(`cut -d $'\n' -f 1-`)
echo ${arr[@]}

Method 5

read -ra arr < <(paste -sd " ")
echo "${arr[@]}"

Slice an Array

Given a list of countries, each on a new line, your task is to read them into an array. Then slice the array and display only the elements lying between positions 3 and 7, both inclusive. Indexing starts from 0.

declare -a arr=(`cut -d $"\n" -f 1`)
echo "${arr[@]:3:5}"

Filter an Array with Patterns

We now transition to some basic examples of bash scripting for the purpose of text processing and data munging. In this challenge, we practice reading and filtering an array.

Method 1

countries=($(cat))
filtered=(${countries[@]/*[Aa]*/})
echo "${filtered[@]}"

Method 2

readarray -t countries
filtered_countries=()
for country in "${countries[@]}"; do
  if [[ ! "$country" =~ [aA] ]]; then
    filtered_countries+=("$country")
  fi
done
for filtered_country in "${filtered_countries[@]}"; do
  echo "$filtered_country"
done

Concatenate an Array with itself

Given a list of countries, each on a new line, your task is to read them into an array. Then, concatenate the array with itself (twice) - so that you have a total of three repetitions of the original array - and then display the entire concatenated array, with a space between each of the countries' names.

Method 1

countries=($(cat))
twice=("${countries[@]}" "${countries[@]}" "${countries[@]}")
echo "${twice[@]}" 

Method 2

countries=($(cat))
echo "${countries[@]} ${countries[@]} ${countries[@]}"

Display an element of an Array

Given a list of countries, each on a new line, your task is to read them into an array and then display the element indexed at 3. Note that indexing starts from 0.

countries=($(cat))
echo "${countries[3]}"

Count the number of elements in an Array

Given a list of countries, each on a new line, your task is to read them into an array and then display the count of elements in that array.

countries=($(cat))
echo "${#countries[@]}"

Remove the first capital letter from each element

You are given a list of countries, each on a new line. Your task is to read them into an array and then transform them in the following way:

The first capital letter (if present) in each element of the array should be replaced with a dot ('.'). Then, display the entire array with a space between each country's names.

declare -a arr
while read -r line
do
    if [[ $line =~ ^[A-Z] ]]
    then 
        temp=$(echo $line | cut -c 2-)
        arr+=(".${temp}")
    fi
done
echo ${arr[@]}

Lonely Integer - Bash!

There are N integers in an array A. All but one integer occur in pairs. Your task is to find the number that occurs only once.

Input Format

The first line of the input contains an integer N, indicating the number of integers. The next line contains N space-separated integers that form the array A.

read -r N
read -ra A
echo ${A[@]} | tr " " "\n" | sort | uniq -u

Last updated