Bash Filter Array



Bash Filter Array

In Bash scripting, filtering an array is useful for various tasks that require extracting specific elements from an array based on certain conditions.


Syntax

filtered_array=()
for element in "${array[@]}"; do
    if [ condition ]; then
        filtered_array+=("$element")
    fi
done

The basic syntax involves using a for loop to iterate over the elements of the array and conditionally add elements to a new array.


Example Bash Filter Array

Let's look at some examples of how to filter an array in Bash:

1. Filter Array to Extract Even Numbers

This script initializes an array with numeric elements, filters the array to extract even numbers, and prints the filtered array.

#!/bin/bash

array=(1 2 3 4 5 6)
filtered_array=()
for element in "${array[@]}"; do
    if (( element % 2 == 0 )); then
        filtered_array+=("$element")
    fi
done
echo "Filtered array (even numbers): ${filtered_array[@]}"

In this script, the array variable array is initialized with the elements 1, 2, 3, 4, 5, and 6. The for loop iterates over each element in the array, and the if statement checks if the element is even. If true, the element is added to the filtered_array variable. The script then prints the filtered array.

Filter array to extract even numbers in Bash

2. Filter Array to Extract Strings Containing a Specific Substring

This script initializes an array with string elements, filters the array to extract strings containing the substring 'apple', and prints the filtered array.

#!/bin/bash

array=("apple pie" "banana split" "apple tart" "cherry pie")
filtered_array=()
for element in "${array[@]}"; do
    if [[ "$element" == *"apple"* ]]; then
        filtered_array+=("$element")
    fi
done
echo "Filtered array (strings containing 'apple'): ${filtered_array[@]}"

In this script, the array variable array is initialized with the elements 'apple pie', 'banana split', 'apple tart', and 'cherry pie'. The for loop iterates over each element in the array, and the if statement checks if the element contains the substring 'apple'. If true, the element is added to the filtered_array variable. The script then prints the filtered array.

Filter array to extract strings containing 'apple' in Bash

3. Filter Array with Elements from User Input

This script initializes an array with elements from user input, filters the array based on user-specified conditions, and prints the filtered array.

#!/bin/bash

array=()
while true; do
    read -p "Enter an element (or 'done' to finish): " element
    if [ "$element" == "done" ]; then
        break
    fi
    array+=("$element")
done

read -p "Enter the substring to filter by: " substring
filtered_array=()
for element in "${array[@]}"; do
    if [[ "$element" == *"$substring"* ]]; then
        filtered_array+=("$element")
    fi
done
echo "Filtered array (strings containing '$substring'): ${filtered_array[@]}"

In this script, the array variable array is initialized as an empty array using (). The user is prompted to enter elements, which are added to the array using the += operator. When the user types 'done', the loop exits. The user is then prompted to enter a substring to filter by. The for loop iterates over each element in the array, and the if statement checks if the element contains the specified substring. If true, the element is added to the filtered_array variable. The script then prints the filtered array.

Filter array with elements from user input in Bash

Conclusion

Filtering an array in Bash is a fundamental task for extracting specific elements from an array based on certain conditions in shell scripting. Understanding how to filter arrays can help you manage and manipulate arrays effectively in your scripts.