The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Comparing data in file with values in table Mohit623 Shell Programming and Scripting 0 01-22-2008 04:57 AM
Comparing 2 list and deleting deuplicate entries eltinator Shell Programming and Scripting 10 08-15-2007 10:35 AM
Problem comparing 2 files with lot of data rafisha Shell Programming and Scripting 4 07-25-2007 04:56 PM
Comparing a distinct value in 1 list with another list manualvin Shell Programming and Scripting 6 06-22-2004 03:42 AM
getting data list into a hash array topcat8 Shell Programming and Scripting 5 03-09-2004 08:02 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-03-2003
Registered User
 

Join Date: Sep 2001
Location: Phoenix
Posts: 76
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Comparing data list...

I have a list of files that I want to compare to another list of files, how do I do that?

The first list will be my known list and hard coded, for example:

mylist="janfile.tar jarfile.jar jan.rpt.Z" etc.

The second list will be found by doing an 'ls' piped to a file:

ls > filelist.dat

I want to compare the list of files in 'mylist' to 'filelist.dat' and only send an email/warning if these files exist...any easy way to do that?

I tried searching the forum for this and found some ideas..but it doesn't seem to work and also, for me 'mylist' is going to be more than the results of 'filelist.dat' so this is probably reversed but I don't know how to tackle it.
Code:
exec < $HOME/filelist.dat
IFS=' '
while read afile ; do
   #Not sure if below syntax will work:
   for each in $mylist; do
      IFS=' '
      if [[ $afile == $mylist ]]; then
       #Send warning.
      else
       #No warning.
      fi
   done 
done
For me, as long as ONE item in mylist (assume 50 files)is found in filelist.dat (about 30 files) I want to halt and warn the user.

Gianni

added code tags for readability --oombera

Last edited by oombera : 02-18-2004 at 06:11 PM.
Forum Sponsor
  #2 (permalink)  
Old 03-03-2003
TioTony's Avatar
Bit Pusher
 

Join Date: Oct 2001
Location: Southern California
Posts: 332
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Since you are only concerned if at least 1 file exists this may work for you:
Code:
mylist="janfile.tar jarfile.jar jan.rpt.Z" 
for i in $mylist
do
   if [ -e $i ]
   then
     echo found $i   #or e-mail and halt
     exit 0
   fi
done
The -e just checks to see if the file exists. If it does, then the expression is true and the echo is run.

added code tags for readability --oombera

Last edited by oombera : 02-18-2004 at 06:11 PM.
  #3 (permalink)  
Old 03-04-2003
Registered User
 

Join Date: Sep 2001
Location: Phoenix
Posts: 76
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Thanks for your response. But doesn't the code just check 'mylist'?? It seems like it would always return 'found' to me.

Where is the comparison to the 'filelist'?
I didn't know how the iteration should be handled here using ksh script.

True that it can halt after finding one instance in filelist but it may still have to check the entire list, non? For example, the third file of 50 in mylist could be the 35th file listed in filelist...how do I handle the interation...I've only handle a single while read situation but not a dual case.

Gianni.
  #4 (permalink)  
Old 03-04-2003
TioTony's Avatar
Bit Pusher
 

Join Date: Oct 2001
Location: Southern California
Posts: 332
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
The '-e' in the 'if' statement checks the current directory to see if the file exists. You do not need to build a data structure containing the 'ls' input. You can simply check the directory directly. In the example, what will happen is it will do the following:

if janfile.tar exists, then print found and exit
if janfile.jar exists, then print found and exit
if jan.rpt.Z exists, then print found and exit

There is no reason to compare 'mylist' against every file in the directory. Suppose you have 1000 file in the directory and you only care if janfile.tar exists. Why would you want to compare it to the other 999 files when you can basically have the program asks if it exists or not without having to iterate through each filename? The for loop is looping through the values of mylist and "asking" if those files exist, it does not care about the other file that may or may not be in that directory. Hope that explains it.
  #5 (permalink)  
Old 03-06-2003
Registered User
 

Join Date: Sep 2001
Location: Phoenix
Posts: 76
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Grazie Tio Tony!

Based on your suggestion, I was able to do what I wanted to do. I think my analogy to doing an 'ls' was throwing you off; however, your solution was fine for it. I wanted to simply my question - most of my questions posed here are not actual but similar to what I wanted to achieve.

I was able to build upon your suggestions and now my scripts are working fine.

Ciao!
Google UNIX.COM
Closed Thread



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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



All times are GMT -7. The time now is 06:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102