Linux/Shell script - How to compare 2 arrays based on patterns and get the differences


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Linux/Shell script - How to compare 2 arrays based on patterns and get the differences
# 1  
Old 12-05-2019
Linux/Shell script - How to compare 2 arrays based on patterns and get the differences

I have


FILE 1 (This file has all master columns/headers)
Code:
A|B|C|D|E|F|G|H|STATUS

FILE 2
Code:
A|C|F|I|OFF_STATUS
3|4|5|4|Y
6|7|8|5|Y

Below command give me all headers of FILE 2 into array2.txt file
Code:
paste <(head -1 FILE2.txt | tr '|' '\n')>array2.txt

So I would like to compare FILE1.txt (ARRAY1) to ARRAY2.txt and get columns which does not exists in ARRAY2.txt

Thank you all
Jay
Moderator's Comments:
Mod Comment Please do always wrap your commands, samples in code tags, as per forum rules.

Last edited by RavinderSingh13; 12-06-2019 at 04:14 AM..
# 2  
Old 12-06-2019
using awk:

Code:
awk -F\| '
FNR == NR { col[$0]; next}
{
 for(i=1;i<=NF;i++)
  if(!($i in col)) {
    printf "%s%s",sep, $i;
    sep="|"
 }
 printf "\n"
}
' array2.txt FILE1

Using bash script:
Code:
#!/bin/bash
IFS=\|
A1=( $(<FILE1) )
IFS=$' \t\n'
A2=( $(<array2.txt) )

for((i=0;i<${#A1[@]};i++))
do
  for((j=0;j<${#A2[@]};j++))
  do
      [ "${A1[i]}" = "${A2[j]}" ] && continue 2
  done
  printf "%s%s" "$SEP" "${A1[i]}"
  SEP=\|
done
printf "\n"


Last edited by Chubler_XL; 12-06-2019 at 06:07 AM..
# 3  
Old 12-06-2019
Try also
Code:
IFS="|" read -a A1 <file1
IFS="|" read -a A2 <file2
IFS=$'\n'
<<< "${A1[*]}$IFS${A2[*]}$IFS${A2[*]}" sort | uniq -u
B
D
E
G
H
STATUS

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script to Compare Files and Email the differences

Hi, I have 2 files abc.txt and bdc.txt. I am using $diff -y abc.txt bcd.txt -- compared the files side by side I would like to write a Shell Script to cmpare the files side by side and print the results( which are not matched) in a side by side format and save the results in another... (10 Replies)
Discussion started by: vasuvv
10 Replies

2. UNIX for Beginners Questions & Answers

Compare two big files for differences using Linux

Hello everybody Looking for help in comparing two files in Linux(files are big 800MB each). Example:- File1 has below data $ cat file1 5,6,3 2.1.4 1,1,1 8,9,1 File2 has below data $ cat file2 5,6,3 8,9,8 1,2,1 2,1,4 (8 Replies)
Discussion started by: shanul karim
8 Replies

3. Shell Programming and Scripting

Compare two big files for differences using Linux

Hello everybody Looking for help in comparing two files in Linux(files are big 800MB each). Example:- File1 has below data $ cat file1 5,6,3 2.1.4 1,1,1 8,9,1 File2 has below data $ cat file2 5,6,3 8,9,8 1,2,1 2,1,4 (1 Reply)
Discussion started by: shanul karim
1 Replies

4. Shell Programming and Scripting

Linux shell script to insert new lines based on delimiter count

The input file is a .dat file which is delimited by null (^@ in Linux). On a windows PC it looks something like this (numbers are masked with 1). https://i.imgur.com/nta2Gqp.jpg The entire file is in one row but it has multiple records - each record contains 80 fields i.e. there are 81 counts... (9 Replies)
Discussion started by: digitalnirvana
9 Replies

5. Shell Programming and Scripting

Comparing 2 arrays but ignoring certain patterns

I'm trying to compare 2 array and print the difference at a 3rd file. However how am i going to compare this 2 arrays by ignoring certain patterns: For example: 1st array contains: ctahci cptcsa0 ctsata:25:seed cptcsa1:50:seed ctsata_1:25:seed 2nd array contains: cptcsa0 ctsata... (0 Replies)
Discussion started by: hongping
0 Replies

6. Shell Programming and Scripting

differences in linux and unix shell

hi all, can any one plz tell me that what is the difference between linux shell scripting and unix shell scripting. is there any difference at all?? if yes that what are the differences and how could it be combatted thanks in advance (1 Reply)
Discussion started by: nasir_khan
1 Replies

7. Shell Programming and Scripting

shell script to format file based on specific patterns

Please help me out and drag me out the deadlock I am stuck into: I have a file. I want the statements under a if...then condition be listed in a separate file in the manner condition|statement.Following are the different input pattern and corresponding output parameters.any generic code to... (7 Replies)
Discussion started by: joyan321
7 Replies

8. Shell Programming and Scripting

script to edit strings based on patterns

Hello All, Here is the file which I want to edit. The script should look for DB2 and if found then delete all lines related to DB2 connection string. Is there way this can be done using script ? DB1 = (DESCRIPTION = (SDU = 32768 (enable = broken) (ADDRESS = (PROTOCOL =... (2 Replies)
Discussion started by: deepakc_in
2 Replies

9. Shell Programming and Scripting

Compare two arrays in sh or compare two fields

I want a soultion to compare two arrays in sh with an easy way.I want a solution to synchrose users between different AIX servers where no NIS is available. All users are meant to be same on all 10 servers. So the approach is to consider first server as master user repository and whatever the users... (0 Replies)
Discussion started by: rijeshpp
0 Replies

10. Shell Programming and Scripting

shell: creating different arrays based on function argument

hi, I was wondering if there was a good way to create an array within a function, where the name is based on a passed argument? I tried this: _____________________________ func(){ #take in 1st arg as the arrayname arrayName=$1 let i=0 while read line do arrayName=${line} let i+=1... (5 Replies)
Discussion started by: nix21
5 Replies
Login or Register to Ask a Question