How to store common elements in a new array in Shell

ShellShellBeginner
Practice Now

Introduction

Shell programming offers a powerful and versatile way to manipulate data, including working with arrays. In this tutorial, we will explore how to identify and store the common elements across multiple arrays in a new array, a common task in Shell scripting.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") shell/VariableHandlingGroup -.-> shell/arrays("`Arrays`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/AdvancedScriptingConceptsGroup -.-> shell/read_input("`Reading Input`") subgraph Lab Skills shell/if_else -.-> lab-417516{{"`How to store common elements in a new array in Shell`"}} shell/arrays -.-> lab-417516{{"`How to store common elements in a new array in Shell`"}} shell/for_loops -.-> lab-417516{{"`How to store common elements in a new array in Shell`"}} shell/cond_expr -.-> lab-417516{{"`How to store common elements in a new array in Shell`"}} shell/read_input -.-> lab-417516{{"`How to store common elements in a new array in Shell`"}} end

Introduction to Shell Arrays

In the world of shell scripting, arrays are powerful data structures that allow you to store and manipulate collections of elements. Shell arrays are versatile and can be used for a variety of purposes, such as storing lists of files, user input, or configuration settings.

Understanding Shell Arrays

A shell array is a variable that can hold multiple values. Each value in the array is called an element, and each element is assigned an index number. In shell scripts, arrays are typically denoted by the @ symbol, and individual elements can be accessed using their index numbers.

Here's an example of how to declare and initialize a shell array in Ubuntu 22.04:

## Declare an array
my_array=(apple banana cherry)

## Access individual elements
echo ${my_array[0]}  ## Output: apple
echo ${my_array[1]}  ## Output: banana
echo ${my_array[2]}  ## Output: cherry

In this example, we've created an array called my_array with three elements: apple, banana, and cherry. We can access each element by referencing its index number, starting from 0.

Common Array Operations

Shell scripts provide a range of built-in commands and syntax for working with arrays. Some of the most common array operations include:

  • Appending elements to an array: my_array+=(new_element)
  • Retrieving the length of an array: ${#my_array[@]}
  • Looping through array elements: for element in "${my_array[@]}"; do ... done
  • Searching for elements in an array: if [[ " ${my_array[@]} " == *" element "* ]]; then ... fi

By understanding these basic array operations, you can start to build more complex shell scripts that leverage the power of arrays to store and manipulate data.

Identifying Common Elements in Arrays

When working with multiple arrays, it's often useful to identify the common elements between them. This can be helpful in a variety of scenarios, such as finding duplicate files, merging configuration settings, or performing data analysis.

Using the comm Command

One way to identify common elements in shell arrays is to use the comm command. The comm command compares two sorted files line by line and produces three columns of output: lines unique to the first file, lines unique to the second file, and lines common to both files.

Here's an example of how to use comm to find the common elements between two arrays in Ubuntu 22.04:

## Define two arrays
array1=(apple banana cherry)
array2=(banana cherry orange)

## Convert arrays to temporary files
tmp1=$(mktemp)
tmp2=$(mktemp)
printf '%s\n' "${array1[@]}" > "$tmp1"
printf '%s\n' "${array2[@]}" > "$tmp2"

## Find common elements using comm
comm -12 "$tmp1" "$tmp2"
## Output: banana cherry

## Clean up temporary files
rm "$tmp1" "$tmp1"

In this example, we first define two arrays, array1 and array2. We then convert the arrays to temporary files using the mktemp command and the printf command. Finally, we use the comm command with the -12 options to find the common elements between the two files, which correspond to the common elements between the two arrays.

Using Set Operations

Another way to identify common elements in shell arrays is to use set operations, such as intersection or union. You can achieve this using various shell constructs and built-in commands.

Here's an example of how to find the common elements between two arrays using set intersection in Ubuntu 22.04:

## Define two arrays
array1=(apple banana cherry)
array2=(banana cherry orange)

## Find common elements using set intersection
common_elements=()
for element in "${array1[@]}"; do
    if [[ " ${array2[*]} " == *" $element "* ]]; then
        common_elements+=("$element")
    fi
done

echo "${common_elements[@]}"
## Output: banana cherry

In this example, we loop through the elements of array1 and check if each element is present in array2 using the [[ ... ]] construct. If an element is found in both arrays, we add it to the common_elements array.

By understanding these techniques for identifying common elements in shell arrays, you can build more powerful and efficient shell scripts that work with complex data structures.

Storing Common Elements in a New Array

After identifying the common elements between two or more arrays, the next step is to store those common elements in a new array. This can be useful for various purposes, such as creating a unique list of items, performing further operations on the common elements, or sharing the common data between different parts of your shell script.

Using Set Operations

One way to store the common elements in a new array is to use set operations, similar to the approach we discussed in the previous section. Here's an example of how to do this in Ubuntu 22.04:

## Define two arrays
array1=(apple banana cherry)
array2=(banana cherry orange)

## Find and store common elements in a new array
common_elements=()
for element in "${array1[@]}"; do
    if [[ " ${array2[*]} " == *" $element "* ]]; then
        common_elements+=("$element")
    fi
done

echo "Common elements: ${common_elements[@]}"
## Output: Common elements: banana cherry

In this example, we loop through the elements of array1 and check if each element is present in array2. If an element is found in both arrays, we add it to the common_elements array.

Using the comm Command

Alternatively, you can use the comm command to store the common elements in a new array. Here's an example:

## Define two arrays
array1=(apple banana cherry)
array2=(banana cherry orange)

## Convert arrays to temporary files
tmp1=$(mktemp)
tmp2=$(mktemp)
printf '%s\n' "${array1[@]}" > "$tmp1"
printf '%s\n' "${array2[@]}" > "$tmp2"

## Find and store common elements in a new array
common_elements=($(comm -12 "$tmp1" "$tmp2"))

echo "Common elements: ${common_elements[@]}"
## Output: Common elements: banana cherry

## Clean up temporary files
rm "$tmp1" "$tmp2"

In this example, we first convert the arrays to temporary files using the mktemp and printf commands. Then, we use the comm command with the -12 options to find the common elements between the two files, and store the result in the common_elements array.

By using these techniques, you can easily store the common elements between multiple arrays in a new array, which can be further used in your shell scripts for various purposes.

Summary

By following the steps outlined in this tutorial, you will learn how to effectively identify and extract the common elements from multiple arrays and store them in a new array using Shell programming. This knowledge will empower you to streamline your Shell scripts and enhance their functionality.

Other Shell Tutorials you may like