FUNDAMENTALS A Complete Guide for Beginners
![icon linux](https://linuxsimply.com/wp-content/uploads/2024/05/Linux-Mascot.png)
You can use the following methods to filter an array in Bash:
- Using for Loop:
for (( i = 0; i < array_length; i++ )); do
#filter according to condition or pattern; done
- Using awk command:
filtered=($(printf "%s\n" "${array[@]}" | awk '/pattern/'))
- Using sed command:
filtered=($(printf "%s\n" "${array[@]}" | sed -n '/pattern/p'))
- Using grep Command:
filtered=($(printf "%s\n" "${array[@]}" | grep 'pattern'))
To filter an array in Bash is to select a subset of elements from the array based on a specified condition such as a particular pattern, meeting any numeric conditions, etc. defined by the user and is often available for manipulating the array on specific requirements. This article discusses 8 effective methods in-depth to filter an array in Bash including loops, commands, and functions
1. Using the “for” Loop
The bash for loop iteratively accesses each element and filters out them based on the given condition effectively filtering the array. Here’s an example:
#!/bin/bash
#Original array
array=("apple" "banana" "orange" "kiwi" "grape")
echo "original: ${array[@]}"
# Select only every second element of the array
filtered_array=()
for (( i=0; i<${#array[@]}; i+=2 )); do
filtered_array+=("${array[$i]}")
done
# Print the filtered array
echo "Filtered array:"
for item in "${filtered_array[@]}"; do
echo "$item"
done
The above code works upon the array with 5 items. it uses the loop for (( i=0; i<${#array[@]}; i+=2 ))
to access elements at indices 0,2, and 4 and store them (apple orange grape) in a new array called filtered_array. This effectively filters the array without altering the original array and it’s seen after printing the arrays using the echo command.
2. Using “for” Loop with If Conditional
The for loop can filter a Bash array more selectively using conditional logic. This allows the loop to filter out elements of an array based on a given pattern.
Here’s the Code:
#!/bin/bash
# Original array declare and print
raw_array=("roger" "hurricane" "lewis" "blackCaps")
echo "original: ${raw_array[@]}"
# Define the pattern to match
pattern="*a*"
echo
# Initialize an empty array to store filtered elements
filtered=()
# Iterate over each element in the array
for item in "${raw_array[@]}"; do
# Check if the element matches the pattern
if [[ "$item" == $pattern ]]; then
# If the element matches the pattern, add it to the filtered array
filtered+=("$item")
fi
done
# Print the filtered array
echo "filtered: ${filtered[@]}"
In the above script, the for loop iterates over the array elements called raw_array. Each ongoing iteration checks if the current item holds the letter “a” and matches the pattern *a* (i.e. any word containing the letter a). If found, it appends the item to the new filtered_array keeping the original array untouched.
3. Using “while” Loop
Apart from the for loop, the Bash while is another powerful and effective method to filter an array via specified pattern-matching by accessing elements iteratively. The syntax to iterate is while [ $index -lt ${#my_array[@]} ]
.
Look at the following example to filter a Bash array using while loop:
#!/bin/bash
# Original array
original_array=("apple" "banana" "orange" "kiwi" "grape")
# Print the array
echo "original:${original_array[@]}"
# Initialize an empty array to store filtered elements
filtered_arr=()
# Define the filtering condition
filter_condition="an"
# Iterate over each element in the array
index=0
while [ $index -lt ${#original_array[@]} ]; do
item="${original_array[$index]}"
# Check if the element meets the filtering condition
if [[ "$item" == *"$filter_condition"* ]]; then
# If the condition is met, add the element to the filtered array
filtered_arr+=("$item")
fi
# Increment the index
((index++))
done
# Print the filtered array
echo "filtered: ${filtered_arr[@]}"
The above script first declares an array with elements and an empty array to store the filtered elements. Then it iterates over the array, stores the current element in the item variable, and checks if the array item contains the pattern “an” using if [[ "$item" == *"$filter_condition"* ]]
where the filter_condition is the pattern “an”. If found, it appends the item to the new filtered_arr and does so till the end of the array. Finally, the printing of the array with filtered items verifies the operation.
4. Using the “awk” Command
The awk command is a useful text-processing tool in Unix/ Linux environments. In addition, it can filter the elements of an array in Bash by converting the array into a string representation, filtering the elements, and converting the filtered string to an array again. Here’s how:
#!/bin/bash
# Example array
real_array=("jellyBean" "apple" "pie" "kiwi" "frappy")
echo "original: ${real_array[@]}" #print array
# Convert the array to a newline-separated string
array_string=$(printf "%s\n" "${real_array[@]}")
# Use awk to filter elements containing the letter "a"
filtered_string=$(printf "%s\n" "$array_string" | awk '/a/')
# Convert the filtered string back to an array
filtered_array=($filtered_string)
echo
# Print the filtered array
echo "Filtered array: ${filtered_array[@]}"
The above script takes an array using real_array=("jellyBean" "apple" "pie" "kiwi" "frappy")
and then it converts the array into a string. Eventually, from the string, it filters out the strings containing the pattern “a” using the command awk '/a/'
and stores them in filtered_string. Finally the filtered string is converted to an array called filtered_array where the filtered elements are stored.
5. Using the “sed” Command
The sed command stands for “steam editor” which is a useful tool for text processing. However, this is capable of filtering a Bash array by applying pattern-based filtering to a newline-separated string representation of the array using syntax $(printf “%s\n” “$input_string” | sed -n ‘/pattern/p’).
Here’s how to filter an array in Bash using the sed command:
#!/bin/bash
# Example array
original_array=(1 2 3 4 5)
#print the array
echo "main array:${original_array[@]}"
# Convert the array to a newline-separated string
array_string=$(printf "%s\n" "${original_array[@]}")
# Use sed to filter odd numbers
filtered_string=$(printf "%s\n" "$array_string" | sed -n '/[13579]/p')
# Convert the filtered string back to an array
filtered_array=($filtered_string)
# Print the filtered array
echo "Filtered array of odd numbers: ${filtered_array[@]}"
The above code takes an array named original_array with 5 numbers. Then it’s converted into a new line-separated string using $(printf "%s\n" "${original_array[@]}")
. After that the sed command filters odd numbers based on the pattern ‘/[13579]/p’ which matches any digit from 1 to 9 and stores the filtrered_string. Finally, the string is again converted to an array to get the filtered_array with selected items.
6. Using the “grep” Command
The Linux grep command is another useful text processing tool in Linux. In addition, it allows users to filter array items based on a given pattern from the string representation of the array to filter using the syntax $(printf “%s\n” “${items[@]}” | grep ‘pattern’. Here, pattern is the filtering expression to match. Here’s how:
#!/bin/bash
# Example array
items=("egg" "banana" "orange" "kiwi")
#print the array
echo "main array:${items[@]}"
# Use grep to filter elements containing the letter "a"
filtered_array=($(printf "%s\n" "${items[@]}" | grep 'a'))
# Print the filtered array
echo "Filtered array:${filtered_array[@]}"
After initializing the items array with 4 elements, the above script uses the grep command and filters elements that contain the letter “a” from the string representation of the array. If found, the matched strings are stored as filtered_array elements. This is visible after printing the arrays using the “echo” command.
7. Using a C-style for Loop
The C-style for loop in Bash constructs a C-programming language style loop with initialization, condition, and increment (or decrement) with the syntax for ((initialize; condition; increment)). It is a unique tool iterating over the array items using their index numbers and eventually obtaining the specified tasks such as filtering an array out.
See the below example to filter an indexed array in Bash using the C-style for loop:
#!/bin/bash
# Example array
numbers=(1 2 3 4 5)
echo "original array: ${numbers[@]}"
# Initialize an empty array to store filtered elements
filtered_array=()
# Get the length of the array
array_length=${#numbers[@]}
# Iterate over each index of the array using a C-style for loop
for (( i = 0; i < array_length; i++ )); do
number="${numbers[$i]}"
# Check if the number is odd
if (( $number % 2 != 0 )); then
# If the number is odd, add it to the filtered array
filtered_array+=("$number")
fi
done
# Print the filtered array
echo "Filtered odd numbers: ${filtered_array[@]}"
Here, the C-style loop for (( i = 0; i < array_length; i++ ))
iterates over the array elements using indices and with each iteration, it filters out the odd numbers using the modulo syntax if (( $number % 2 != 0 ))
. If true, the if block executes and appends the odd numbers to the filtered_array. Finally, the success of the filtering operation is visible after printing the elements using the echo command.
8. Using a Bash Function
The Bash function can combine the necessary commands and expressions for tasks such as filtering a Bash array and execute them by calling the function where the array is passed as the parameter.
Now. let’s see a function in action to filter an array in Bash:
#!/bin/bash
# Define a function to filter the array
filter_array() {
local input_array=("$@") # Get all arguments as an array
local filtered_array=() # Initialize an empty array to store filtered elements
# Iterate over each element in the input array
for item in "${input_array[@]}"; do
# Apply filtering logic here
if [[ $item == *"a"* ]]; then
# If the element meets the criteria, add it to the filtered array
filtered_array+=("$item")
fi
done
# Return the filtered array
echo "${filtered_array[@]}"
}
# Example array
array=("apple" "banana" "peech" "kiwi")
# Call the function to filter the array
filtered_result=$(filter_array "${array[@]}")
# Print the filtered array
echo "Filtered array: $filtered_result"
Here, the above script defines a function named filter_array to take an array as an argument. Inside the scope of the function, the filter logic is defined in such a way that the for loop iterates through the array items and matches any items containing the character “a” employing the wildcard pattern *a*, and eventually adds it to the filtered_array. Finally, the array=("apple" "banana" "peech" "kiwi")
is passed to the function as its argument and the echo command verifies the filtering operation by printing items on the terminal.
Filtering an Associative Array in Bash
Bash is also capable of filtering an associative array based on the given condition (on the key or value) by iterating over the key-value pairs. Look at the example below to filter an associative array in Bash:
#!/bin/bash
#declare associative arrays
declare -A original_array
declare -A filtered_array
# Populate the original array
original_array[fruit]="apple"
original_array[color]="red"
original_array[number]=42
original_array[city]="Tokyo"
# Filter based on key patterns
pattern="co" # Example pattern to match keys starting with "co"
for key in "${!original_array[@]}"; do
if [[ $key =~ ^$pattern ]]; then
filtered_array["$key"]="${original_array[$key]}"
fi
done
# Print the filtered associative array
echo "Filtered array:"
for key in ${!filtered_array[@]}; do
echo "$key: ${filtered_array[$key]}"
done
The above code first declares 2 associative arrays and the original array is initialized with 4 key-value pairs. Then a for loop iterates over the “original_array” keys and checks if any key contains the pattern “co” (pattern=”co”) using if [[ $key =~ ^$pattern ]]
. If matched, the corresponding key-value pair is stored in the “filtered_array”. Finally, the echo command prints the filtered key-value pairs.
Conclusion
This article discusses 8 different methods to filter an array in Bash. It mentions iterative methods using for loop, and the while loop to filter an array in Bash. It also covers several Linux commands such as awk, sed, and grep to filter an array in Bash including how to filter a Bash array with a function. Furthermore, it sheds light on filtering associative arrays in Bash. Hope this article equips you with several approaches to filter a Bash array and elevate your data handling and manipulation mastery within arrays.
People Also Ask
What is meant by Filtering an Array in Bash?
Filtering an array in Bash refers to the process of extracting a subset of elements meeting a user-defined condition. It typically involves filtering out array elements based on the condition and storing them in a new filtered array. This allows for advanced data handling and manipulation of data within arrays while keeping the source untouched.
Can I filter the indices of an array in Bash?
Yes, you can. You can use a for loop for example to iterate over the array indices and apply filtering conditions as needed. This will let you work with the array index numbers and eventually filter the indices.
Are there any built-in methods to filter an array in Bash?
No. There are no built-in methods or functions to filter an array in Bash. Instead, you can use several methods such as using loops or commands such as “awk”, “sed”, “grep” to filter an array in bash. For example, to filter the array cars=(audi honda subaru)
and filter out the elements audi and subaru, you can use the “awk” command with the syntax: filtered_array=($(printf "%s\n" "${cars[@]}" | awk '/u/'))
where the elements will be stored in the filtered_array.
What is the difference between filtering an array and removing elements from an array in Bash?
Filtering an array in Bash means extracting array elements based on user-defined filtering criteria or patterns. This typically isolates elements meeting specific requirements in a new array without modifying the original one. On the other hand, removing elements from an array is the process of deleting certain elements from the array entirely which directly modifies the original array.
Related Articles
- How to Convert a JSON Array into Bash Array [5 Methods]
- Master Bash Array of Strings with Spaces [Complete Guide]
- How to Convert String into Array in Bash [8 Methods]
- How to Append to an Array in Bash? [4 Easy Methods]
- How to Clear an Array in Bash [6 Methods]
- How to Copy an Array in Bash [6 Simple Methods]
- How to Reverse an Array in Bash? [8 Easy Methods]
- How to Slice an Array in Bash [10 Simple Methods]
- Iterate Through a Bash Array Using “foreach” Loop [5 Examples]
- How to Check If an Array is Empty in Bash? [5 Methods]
<< Go Back to Array Operations in Bash | Bash Array | Bash Scripting Tutorial