filter in bash

Table of Contents

1. for over list and take only the filtered

#!/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

original: apple banana orange kiwi grape Filtered array: apple orange grape

*

Backlinks

bash

#!/bin/bash
cd ~/Dropbox/Notes/RoamNotes
array=()
filtered_array=()
mapfile -d $'\0' array < <(find . -print0 )

for (( i=0; i<${#array[@]}; i+=1 )); do
filtered_array+=$(stat "${array[$i]}")
done



echo ${array[2]}
echo ${filtered_array[2]}

Author: Linfeng He

Created: 2024-04-03 Wed 20:16