The UNIX and Linux Forums  

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



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
search two words in sed ajitkumar2 Shell Programming and Scripting 12 08-01-2008 04:00 AM
similar words Streetrcr UNIX for Dummies Questions & Answers 1 03-22-2008 09:24 AM
sed [delete everything between two words] Orbix UNIX for Dummies Questions & Answers 3 12-25-2007 08:27 AM
Get the words.. kakashi_jet Shell Programming and Scripting 10 07-31-2006 10:30 AM
Looking for Words File OC Del Guy UNIX for Dummies Questions & Answers 9 04-12-2006 08:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-18-2009
luna_soleil luna_soleil is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 33
Comparing 2 words

Hi

I have assigned list of arrays as below:
rtab[1]="TAB1 Y"
rtab[2]="TAB2 Y"
rtab[3]="TAB3 Y"
rtab[4]="TAB4 Y"

And I have a text file comp.txt that contains
>vi comp.txt
TAB1 Y
TAB2 Y
TAB3 Y
TAB4 Y

I want to compare the content of the array with the content in comp.tx.. so I have written the code as below :

i=0
while read ttab
do
arr[$((i=i+1))]=$ttab
if [ $ttab = ${rtab[i]} ]
then
echo "${rtab[i]} -> status is Y [PASS]"
else
echo "${rtab[i]} -> status is N [FAIL]"
fi
done < comp.txt

But unfortunately I've been getting output ..status is N [FAIL] everytime i run this script.
Is this because the script can't compare 2 words TAB Y ("TAB2 Y" compare with "TAB2 Y" is not permitted, "TAB" compare with "TAB" is OK)?
If yes, how do I rectify this?

Thank u very much

Last edited by luna_soleil; 03-18-2009 at 02:06 PM..
  #2 (permalink)  
Old 03-18-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,805
arrays start the index from zero not one.

If you want to prove existence of one of the array elements in the file comp.txt
using grep:

Code:
grep -q -e "${rtab[1]}"  -e "${rtab[2]}" -e "${rtab[3]}" -e "${rtab[4]}" -e "${rtab[4]}" comp.txt
if [[ $? -eq 0 ]] ; then
  echo "found"
else
  echo "not found"
fi

  #3 (permalink)  
Old 03-18-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,342
Quote the variables in this statement:


Code:
if [ "$ttab" = "${rtab[i]}" ]

Regards
  #4 (permalink)  
Old 03-18-2009
luna_soleil luna_soleil is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 33
Thanks jim and franklin for your help.. I've learnt new things and get my code fixed from you two.. thank u so much
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 On




All times are GMT -4. The time now is 01:40 AM.


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-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0