Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-08-2006
Registered User
 

Join Date: Jun 2006
Posts: 5
Comparing Arrays?

Is there anyway that I can compare two Arrays to see if any new strings have been added in them? eg:

Array 1: Joe Bob Jane
Array 2: Joe Bob Jane Greg

It would then output a new array with the changes:

Array 3: Greg

I'm not very good at shell scripting, and my google and forum searches have come up empty. I'd rather do the whole thing in bash, because this will be run on OSX, and you don't really get much else than bash unless you install developer tools, and the computers that I intend to use the script on will not have the develope tools.

Thanks in advance!
Sponsored Links
  #2 (permalink)  
Old 06-09-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,431
Perhaps just use strings intead of arrays, e.g....
Code:
#!/usr/bin/bash

a1="Joe Bob Jane"
a2="Joe Bob Jane Greg"
a3=${a2#$a1}
echo $a3

  #3 (permalink)  
Old 06-09-2006
Registered User
 

Join Date: Jun 2006
Posts: 5
The problem with that is that if I try to do this (which is what will end up happening):

Code:
#!/bin/sh

a1="Joe Bob Jane"
a2="Joe Bob Jack Jane Greg"
a3=${a2#$a1}
echo $a3

The output I get is this:

Code:
Joe Bob Jack Jane Greg

And I need it to only return the words in the second string that weren't in the first, which is why I thought arrays would be better.
  #4 (permalink)  
Old 06-09-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,431
This should only return the words in the second string that weren't in the first...
Code:
#!/usr/bin/bash

a1="Joe Bob Jane"
a2="Joe Bob Jack Jane Greg"
a3=$(printf "$a1\n$a2"|awk '{for(i=1;i<=NF;i++)if(NR==1)a[$i]=i;else if(!($i in a))print $i}')
echo $a3

  #5 (permalink)  
Old 06-09-2006
Registered User
 

Join Date: Jul 2005
Posts: 137

Code:
ruby -e 'puts (ARGV.last.split -
 ARGV[-2].split).join(" ")' "foo bar" "zed foo bar zab"
zed zab


Code:
newlisp -e '(silent)(println (join (difference
 (parse (last (main-args))) (parse (nth -2 (main-args)))) " "))' "foo bar" "zed foo bar zab"
zed zab

The newlisp executable, which is all you need, is about 170kb on my machine. You could put it on a floppy or a flash drive.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need Help with awk and arrays fusionX Shell Programming and Scripting 7 02-11-2008 06:41 PM
awk arrays imonthejazz Shell Programming and Scripting 1 09-21-2007 10:29 AM
arrays in awk??? craigsky Shell Programming and Scripting 3 08-27-2007 10:13 PM
KSH and arrays whited05 Shell Programming and Scripting 1 06-24-2005 01:07 PM
Two or more arrays in Awk nitin UNIX for Advanced & Expert Users 1 12-10-2001 09:37 PM



All times are GMT -4. The time now is 08:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0