UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 18th, 2005, 07:40 AM
roopa_arju roopa_arju is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 6 roopa_arju User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 37 m 38 sec
Reputation Power: 0
Question If statement to compare strings

Hi,
I have a file report1.txt which has the contents as:
SheetName|RecordName|FieldName|TableName|ColumnName|Change
Summary||2||No specification received for output file record|MODIFIED
Central Checks|R3002SPLDXVOLUME|F3002-VOL1-VOL-SRL-NUM|SUBMISSION|SERIAL_NUM|DEL
ETED
Suspense data|RC3310SUSPMIGXDATA||||ADDED
Suspense data|RC3310SUSPMIGXDATA|FC3310-RECORD-TYPE|||ADDED

and my unix code is:

#!/bin/ksh
set -x
cat report1.txt | while read LINE
do
recordname=`echo $LINE | awk -F"|" '{print $2}'`
change=`echo $LINE | awk -F"|" '{print $6}'`
if test ! "$recordname" || test "$change" = "ERROR"
then
echo "something"
fi
done
set +x

I am checking whether recordname field is empty or change field contains string "ERROR".
But while executing it is not assigning the change value in if condition only recordname value is correctly assigned.

It is giving the error as follows:

changefield=ADDED
+ err=ERROR
+ [ ! RC3310SUSPMIGXDATA ]
= ERROR ]
Tell me the reason

Reply With Quote
  #2  
Old August 19th, 2005, 09:53 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,089 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 3 h 39 m 54 sec
Reputation Power: 9
Code:
#!/bin/sh


#  IF  is not the tool for string compare
#  AWK for split a luxus, make once the loop
#  the PIPE delimiter hard to correctly controll, be save, change it

#  SED is also a good way to do that
#  i never write ksh-scripts, imo your if statement is buggy
#  in /bin/sh: [ -z "$aaa" -o "$bbb" = ERROR ] 

for what in `cut -d'|' -f2,4 data | tr '|' '!'`
do 
echo $what
case $what in !) echo both fields ARE EMPTY
;;              !?*) echo field2 IS EMPTY
;;              ?*!) echo field4 IS EMPTY
;;                *) echo both fields are NOT EMPTY
;; esac
done
__________________
working on Solaris[5-9], preferred languages french and C.

Reply With Quote
  #3  
Old August 22nd, 2005, 12:24 AM
roopa_arju roopa_arju is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 6 roopa_arju User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 37 m 38 sec
Reputation Power: 0
Question

I agree with ur code.
But the thing is i have to store the second and fourth field in separate variables for some validation and also to search for those variables in a file.So tell me how to store them separately.

And how will i check whether the fourth filed value is equal to ERROR or not.
The code [ -z "$aaa" -o "$bbb" = ERROR ]
is not working.Its not checking $bbb value.Please tell me the reason.

Last edited by roopa_arju : August 22nd, 2005 at 01:32 AM. Reason: Needed to include more query

Reply With Quote
  #4  
Old August 22nd, 2005, 02:33 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,089 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 3 h 39 m 54 sec
Reputation Power: 9
Quote:
Originally Posted by roopa_arju
I agree with ur code.
But the thing is i have to store the second and fourth field in separate variables for some validation and also to search for those variables in a file.So tell me how to store them separately.

And how will i check whether the fourth filed value is equal to ERROR or not.
The code [ -z "$aaa" -o "$bbb" = ERROR ]
is not working.Its not checking $bbb value.Please tell me the reason.


$bbb will never be checked as long $aaa is not empty
to access every field sepatrately
#!/bin/sh
IFS="|"
set `cat inputfile`
while [ $# != 0 ]
do
echo $1
echo $2
echo $3
shift 3
done

PROBL: every inputline MUST contain exactly 3-1 = 2 pipes
else this script will exit an error
i prefer the 'case' way
Code:
field2=EMPTY
field4=EMPTY
case $what in !) echo both fields ARE EMPTY
;;     !*ERROR*) echo ERROR FOUND field2 is EMPTY
;;  ?*!*ERROR*) echo ERROR FOUND field2 is NOT empty
                      field2=`echo $what|sed 's/!.*//'
;;              !?*) echo field2 IS EMPTY
                      field4=`echo $what|sed 's/!//'
;;              ?*!) echo field4 IS EMPTY
                      field2=`echo $what|sed 's/!//'
;;                *) echo both fields are NOT EMPTY
                      field2=`echo $what|sed 's/!.*//'
                      field4=`echo $what|sed 's/.*!//'
;; esac
#now check the fields, and prefer case to if

# nota: cut and case are very fast
# sed will work on a short string only if it has to work on it :)

Last edited by guggach : August 22nd, 2005 at 02:40 AM.

Reply With Quote
  #5  
Old August 22nd, 2005, 06:59 AM
roopa_arju roopa_arju is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 6 roopa_arju User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 37 m 38 sec
Reputation Power: 0
Thank you very much.
Now i know how to handle my problem.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > If statement to compare strings


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway