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
Method 2
Method 3
Method 4
Method 5
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.
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
Method 2
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
Method 2
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.
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.
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.
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.
Last updated