|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
Quote:
$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. |
|
#5
|
|||
|
|||
|
Thank you very much.
Now i know how to handle my problem. |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > If statement to compare strings |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|