How to do index to index value comparison in Unix

codeholic24

I have a scenario where i did array_1 [index] value check with array_2 [index] value.

My below code is working like a charm, but is there any different approach to achieve the same?

I'm looking for a unique way of handling 2 array list values and compare, with few lines of code.

My code:

#!/bin/bash

array_1=(4 4 3)
array_2=(4 1 3)

i=''
j=''

count=0
for i in "${array_1[@]}"
do 
 i="cmd ${array_1[$count]}"
 j="cmd ${array_2[$count]}"
   
     if [ "$i" -eq "$j" ]
     then
           echo "${array_1[$count]} match with ${array_2[$count]}"
           count=$(( count + 1 ))        
     else 
           echo "${array_1[$count]} does not match with ${array_2[$count]}"
           exit 1
     fi 
      
done 

Note: Code is checked with shellcheck.net , no error found.

If two arrays have different values, below is the output:

array_1=(4 4 3)
array_2=(4 1 3)

Output:

4 match with 4
4 does not match with 1

If two arrays have the same values, below is the output:

array_1=(4 4 3)
array_2=(4 4 3)

Output:

4 match with 4
4 match with 4
3 match with 3
dawg

You added awk to your question.

With awk you could do:

awk 'FNR==NR{x[FNR]=$1; next}
     x[FNR]==$1{print x[FNR] " match with " $1; next}
     {print x[FNR] " does not match with " $1}
     ' <(printf "%s\n" "${array_1[@]}") <(printf "%s\n" "${array_2[@]}")

Or with paste and awk you can do:

paste <(printf "%s\n" "${array_1[@]}") <(printf "%s\n" "${array_2[@]}") | awk '
$1==$2{print $1 " match with " $2; next}
{print $1 " does not match with " $2}'

Either prints (with the two arrays in your example):

4 match with 4
4 does not match with 1
3 match with 3

If you want to exit after the first no match, just add exit after the print:

awk 'FNR==NR{x[FNR]=$1; next}
     x[FNR]==$1{print x[FNR] " match with " $1; next}
     {print x[FNR] " does not match with " $1; exit 1}
     ' <(printf "%s\n" "${array_1[@]}") <(printf "%s\n" "${array_2[@]}")

Explanations

The construct of printf "%s\n" "${array_1[@]}" creates the output of array_1 as a string separated by \n:

printf "%s\n" "${array_1[@]}"
4
4
3

The construct of:

cmdX <(cmdY)

Does the following:

  1. Creates an anonymous file-like fifo in the shell from the stdout of cmdY and
  2. Feeds a file name to that file-like output into cmdX as an argument to then be read as file.

paste takes two file-like inputs and creates a two column single output:

paste <(printf "%s\n" "${array_1[@]}") <(printf "%s\n" "${array_2[@]}") 
4   4
4   1
3   3

Once you have the arrays side by side or as if two files then comparing the two with awk diff comm perl or ruby is trivial.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP - How do I unset an array index by value inside that index

Python Pandas: Index a value and boolean comparison

why do pandas comparison operators not align on index?

How to achieve Javascript array index comparison?

How to index by value, group by

how do I check an array of tuples for a value in index[0] and if there is a match, display the value at index [1] of the matching tuple

how to asigne index jquery index value in to html

How to get column index of field in unix shell

how do I find the index of an specific value in numpy array?

How do I find the index of the maximum value of an array?

How do I add the name of the subjects rather than the index value?

How do I check in JavaScript if a value exists at a certain array index?

How do i change a single index value in pandas dataframe?

C# How to change list index value in do while loop

How do I find the index of the maximum value in a list in Perl 6?

How do I add a value at a specific index to an empty list in dart?

How do I get an Array item's opposite index and value?

How do I call elements of an array as their index numerical value

How do I get the index of an object in an NSArray using string value?

How do I find the index of a known value in a pandas dataframe column?

How do I get a value by index from a nested table in lua?

How do you select index of the list particular value?

How do I get the index of a row where max value is not duplicated?

How do you get the index of a JSON value in Python?

Dart how do I find the index of true value in the list

How do I find index before and after a value?

How do I return the corresponding index of the maximum value in a range?

How do I find the array index with its highest value with vhdl?

How do i get the value of first index in a returned List of array