Bash script to compare two lists

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to compare two lists
# 1  
Old 08-02-2011
Bash script to compare two lists


I do little bash scripting so sorry for my ignorance.

How do I compare if the two variable not match and if they do not match run a command.

I was thinking a for loop but then I need another for loop for the 2nd list and I do not think that would work as in the real world there could be >= 100 entries in the variables.

$VAR1="me you him her"
$VAR2="you me him"

If VAR2 does not contain an entry in VAR1 create this missing entry and do xyz else do yz.

Last edited by radoulov; 08-02-2011 at 10:53 AM.. Reason: Code tags.
# 2  
Old 08-02-2011
Given you're using bash, you could store your lists in arrays:

The following output:

zsh-4.3.11[t]% bash s
before ...
me      you
you     me
him     him
after ...
me      me
you     you
him     him
her     her

... is produced by the code below:

  me you him her
  you me him

printf 'before ...\n'

paste <( printf '%s\n' "${list1[@]}" ) <( printf '%s\n' "${list2[@]}" )  
for i in "${!list1[@]}"; do
  [[ ${list1[i]} != ${list2[i]} ]] &&

printf 'after ...\n'

paste <( printf '%s\n' "${list1[@]}" ) <( printf '%s\n' "${list2[@]}" )

# 3  
Old 08-02-2011
POSIX shell:
VAR1='me you him her'
VAR2='you me him'
for w in $VAR1; do                                                         
  if ! echo "$VAR2" | grep -q "$w" ; then
    VAR2="$VAR2 $w"
if [ "$changed" ]; then
  echo "changed: VAR1='$VAR1' VAR2='$VAR2'"
  echo "no changed: VAR1='$VAR1' VAR2='$VAR2'"

This User Gave Thanks to yazu For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare date bash script

I all I have written a bash script for compare two date. One of those is a result of query, and another is current date. I have a problem with the format, because the first is 09/12/19 18:50:30 but for having this result I have to do d1DB=$(date -d "$valData" +'%m/%d/%y %T') and the second... (9 Replies)
Discussion started by: rdie77
9 Replies

2. Shell Programming and Scripting

Array compare bash script

Hello, i have a script that should compare between ${ARRAY} that contains all fstab record like this : >>echo ${ARRAY} / /boot between all mountpoints in my df that is stord in ${ARRAY2} >>echo ${ARRAY2} / /boot /dev/shm /var/spool/asterisk/monitor now i have this loop: for i in... (6 Replies)
Discussion started by: batchenr
6 Replies

3. Homework & Coursework Questions

[Python] Compare 2 lists

Hello, I'm new to the python programming, and I have a question. I have to write a program that prints a receipt for a restaurant. The input is a list which looks like: product1 product3 product8 .... In the other input file there is a list which looks like: product1 coffee 5,00... (1 Reply)
Discussion started by: dagendy
1 Replies

4. Shell Programming and Scripting

compare two lists on two files

I have two files A and B listing ip addresses and all the ip addresses in B are in A, and A includes other ip addresses now I want to get the list of the ip addresses that are in A but not in B how to achieve this? thanks (1 Reply)
Discussion started by: esolvepolito
1 Replies

5. Shell Programming and Scripting

Compare two lists with perl

Hi everybody! I'm trying to delete some elements from a list with two elements on each row agreeing with the elements in another list. Pratically I want a perl script able to take each element of the second list (that is a single column list), compare it with both elements of each row from the... (3 Replies)
Discussion started by: gabrysfe
3 Replies

6. Programming

Python: Compare 2 word lists

Hi. I am trying to write a Python programme that compares two different text files which both contain a list of words. Each word has its own line worda wordb wordc I want to compare textfile 2 with textfile 1, and if there's a word in textfile 2 that is NOT in textfile 1, I want to... (6 Replies)
Discussion started by: Bloomy
6 Replies

7. UNIX for Dummies Questions & Answers

Compare 2 lists using a full and/or partial match at beginning of line?

hello all, I wonder if anybody might be able to help with this. I have file 1 and file2. Both files may contain thousands of lines that have variable contents. file1 234GH 5234BTW 89er 678tfg 234 234YT tfg456 wert 78gt gh23444 (7 Replies)
Discussion started by: Garrred
7 Replies

8. Shell Programming and Scripting

Shell Script to Create non-duplicate lists from two lists

File_A contains Strings: a b c d File_B contains Strings: a c z Need to have script written in either sh or ksh. Derive resultant files (File_New_A and File_New_B) from lists File_A and File_B where string elements in File_New_A and File_New_B are listed below. Resultant... (7 Replies)
Discussion started by: mlv_99
7 Replies

9. UNIX for Dummies Questions & Answers

compare 2 very large lists of different length

I have two very large datasets (>100MB) in a simple vertical list format. They are of different size and with different order and formatting (e.g. whitespace and some other minor cruft that would thwart easy regex). Let's call them set1 and set2. I want to check set2 to see if it contains... (2 Replies)
Discussion started by: uiop44
2 Replies

10. Shell Programming and Scripting

Compare lists of files

If I had a list of numbers in two different files, what would be the fastest and easiest way to find out which numbers in list B are not in list A without reading each number in list B one at a time and using grep thousands of times against list A? I have two very long lists of numbers and the... (4 Replies)
Discussion started by: keelba
4 Replies
Login or Register to Ask a Question