Comparing two array in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing two array in shell script
# 1  
Old 03-01-2012
Comparing two array in shell script

In shell script i have two variables with value
Code:
debit_sal="DOG,HIU,JIU,GYT,PPI,KIU,HUY........."
debit_req='HIU, JIU, HUY, GTR, KOI, ............"
 
i stored this two in two array
arr=$(echo $debit_sal| tr "," "\n");
arr1=$(echo $debit_req| tr ", " "\n"); #(note second arry has comma with space)
set -A dfArr $arr
set -A drArr $arr1
echo $dfArrr
echo $drArr
x=${#dfArrr[@]}
y=${#drArrr[@]}

I need compare this two array and find out / print in console what are all codes extra in each array ..
i am not able to proceed the way to find .. help me
example :
debit_sal=DOG,GYT,PPI,KIU
debit_req=GTR,KOI
# 2  
Old 03-01-2012
if i understand your correctly, you want to find elements in both the arrays which is not common. if so,

you need not use arrays for this.

Code:
$ x="DOG,HIU,JIU,GYT,PPI,KIU,HUY"
$ y="HIU,JIU,HUY,GTR,KOI"
$ echo $x | sed 's/,/\n/' > a
$ echo $y | sed 's/,/\n/g' > b
$ sort a b | uniq -u
DOG
GTR
GYT
KIU
KOI
PPI

OR
if you wanted exactly what is extra in each of these arrays:

Code:
$ x="DOG,HIU,JIU,GYT,PPI,KIU,HUY"
$ y="HIU,JIU,HUY,GTR,KOI"
$ echo $x | sed 's/,/\n/g' > a
$ echo $y | sed 's/,/\n/g' > b
$ sort a > a1
$ sort b > b1

extra in first
Code:
$ comm -23 a1 b1
DOG
GYT
KIU
PPI

extra in second

Code:
$ comm -13 a1 b1
GTR
KOI

Guru.

Last edited by guruprasadpr; 03-01-2012 at 07:38 AM.. Reason: added g to sed.
# 3  
Old 03-01-2012
for me it is printing like this
Code:
DOGnHIU,JIU,GYT,PPI,KIU,HUY
HIUn JIUn HUYn GTRn KOI

# 4  
Old 03-01-2012
there were some extra spaces in the assignment to the variable y. Its removed from my original post.

Try again.
# 5  
Old 03-01-2012
i am using HP-UNIX

Code:
$cat test.ksh
 
x="DOG,HIU,JIU,GYT,PPI,KIU,HUY"
y="HIU,JIU,HUY,GTR,KOI"
echo $x | sed 's/,/\n/' > a
echo $y | sed 's/,/\n/g' > b
x="DOG,HIU,JIU,GYT,PPI,KIU,HUY"
y="HIU,JIU,HUY,GTR,KOI"
echo $x | sed 's/,/\n/' > a
echo $y | sed 's/,/\n/g' > b
sort a b | uniq -u

Above is the one i am using but still iam facing the issue

its print likethis

Code:
 
$ ./test.ksh
DOGnHIU,JIU,GYT,PPI,KIU,HUY
HIUnJIUnHUYnGTRnKOI

# 6  
Old 03-01-2012
my mistake:

Code:
x="DOG,HIU,JIU,GYT,PPI,KIU,HUY"
y="HIU,JIU,HUY,GTR,KOI"
echo $x | sed 's/,/\n/g' > a
echo $y | sed 's/,/\n/g' > b
sort a b | uniq -u

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. UNIX for Dummies Questions & Answers

Shell script needed for comparing two files

Hi, I need shell script to compare the two files based on certain fields and output should contains the required fields based on result.pls find sample input files and required output file 1 COUNT, BNG_IP,PORT,OVLAN 22 , 10.238.60.129,1/1,2009 144 , 10.238.60.129,1/1,2251 ... (5 Replies)
Discussion started by: surender reddy
5 Replies

3. Shell Programming and Scripting

Comparing dates in shell script

Hi All, I have a date variable say dt="2014-01-06 07:18:38" Now i need to use this variable to search a log and get the entries which occured after that time. (1 Reply)
Discussion started by: Girish19
1 Replies

4. Shell Programming and Scripting

Need Help in comparing 2 text files in shell script

Hi All, I have 2 files like below vi f1 frog elephant rabit zebra dog vi f2 rabit dog ============== Now i want to comapre two files and the result will be frog (8 Replies)
Discussion started by: kumar85shiv
8 Replies

5. Shell Programming and Scripting

shell script for comparing two files

Hi, I have 2 files as below FILE1.dat co1|co2|co3 abnd|45.56|AZ dkny|30.2|PA sam|.23|VA FILE.CTL FILENAME|FILE1.dat NO OF RECORDS|3 CHECKSUM|75.99 Could you please help me to write a shell script to compare 1. TOTAL RECORDS from FILE1.dat with NO of RECORDS in FILE.CTL 2.... (8 Replies)
Discussion started by: dreamsportsteam
8 Replies

6. Shell Programming and Scripting

Comparing 2 files using shell script

Hi Experts, I have 2 files 1 file consists of 800 records and 2 file consists of 100 records with matching column as Membership_Num.So i need a script which will compare the 2 files and displays the output.As these are the files the script should take any delimter like (tab,comma) as input... (6 Replies)
Discussion started by: naveen.dasu
6 Replies

7. Shell Programming and Scripting

comparing 2 files in shell script

I have 2 files config1h.txt ----------------- BFMU=ENABLE,ID=PM THR=OFF,REP=ALL,CON=IACM,TIM=OFF;GPON collection strategy 1.3.6.1.2.1.2.2:8 1.3.6.1.2.1.2.2:7 1.3.6.1.4.1.637.61.1.35.11.4:4 1.3.6.1.4.1.637.61.1.35.11.4:3 1.3.6.1.4.1.637.61.1.35.10.1:2 1.3.6.1.4.1.637.61.1.35.10.1:43... (7 Replies)
Discussion started by: LavanyaP
7 Replies

8. Shell Programming and Scripting

Comparing 2 file use c shell script

Hi all, I got 2 file : file1: file2: output: But , must write out in C shell.Anybody can help solve? (6 Replies)
Discussion started by: proghack
6 Replies

9. Shell Programming and Scripting

comparing two files using shell script

hi experts please help me to compare two files which are in different directory file1<file will be master file> (/home/rev/mas.txt} ex x1 x2 file2 <will be in different folder> (/home/rev/per/.....) ex x3 x4 the filesinside per folder i need to compare with master file and the files... (2 Replies)
Discussion started by: revenna
2 Replies

10. UNIX for Dummies Questions & Answers

shell script for comparing 2 files

Hi, how to read the 2 files and compare each other in shell script? i have 2 files test1 and test2, both files contains 20 character records.we have to compare file 1 records with file2, if exists then reject the record else we have to append it to test2 file. in file test1 around 100 single... (2 Replies)
Discussion started by: prashanth.spl
2 Replies
Login or Register to Ask a Question