character comparison problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers character comparison problem
# 1  
Old 02-21-2003
character comparison problem

I have three lines in a log that I want to check to make sure that they say the following (mm/dd/yyyy and hh:mm:ss changes depending in when the log is generated):

'*** Testing file for mm/dd/yyyy'
'*** End of procedure! Time was: hh:mm:ss'
'*** End ***'

I did some sed and cut and ended up with something like this (the constant portion):
Code:
if [[ $first = '***            Testing file' ]] &&
   [[ $second = '***            End of procedure Time was:!' ]] &&
   [[ $third = '*** End ***' ]]; then
  do something
else
  handle error
fi

When I echo out the uncut lines (as is) compared to the cut lines, they match up to the point where I did the cut:

'*** Testing file for mm/dd/yyyy'
'*** Testing file for'
'*** End of procedure! Time was: hh:mm:ss'
'*** End of procedure! Time was'
'*** End ***'
'*** End ***'

Technically, my if-then statement matches the cut lines but the else ALWAYS gets executed even though the test echo statements I did seemt to match exactly to the dot...what is going on?? I can't figure it out. I thought it was some blanks somewhere but that did not appear to be the case. Anyone??

Gianni

added code tags for readability --oombera

Last edited by oombera; 02-20-2004 at 10:42 AM..
# 2  
Old 02-21-2003
That's some very creative syntax you have there. I've never seen an "if" statement like that.

But look where you are checking $second. You have mispositioned the explanation point in your constant.
# 3  
Old 02-21-2003
The '!' in the wrong position was a typo but that didn't do anything..I'm still getting the error like it is not matching.

Also, is that wrong to write an if-else-then like that or you're just making an observation. I have tons of codes with that type of if-else-then...hehe

Thanks.
# 4  
Old 02-21-2003
Would be even better if someone can help me figure out how to check for the entire line content, keeping in mind that the pattern would still be:

*** Testing file for mm/dd/yyyy' '*** End of procedure! Time was: hh:mm:ss'

Would this make sense for pattern mm/dd/yyyy in the first line?

[0-1][0-2]/[0-3][0-9]/200[3-9]?

Thanks.
# 5  
Old 02-21-2003
If what you said was true, this should be working. This script
Code:
#! /usr/bin/ksh

first='*** Testing file'
second='*** End of procedure Time was:!'
third='*** End ***'

if [[ $first = '*** Testing file' ]] &&
   [[ $second = '*** End of procedure Time was:!' ]] &&
   [[ $third = '*** End ***' ]]; then
       echo ok
else
       echo not ok
fi
exit 0

will print "ok" when I run it. Give it it try and see if it works for you.

And try:
echo first = \"${first}\"
Maybe you have trailing blanks.
# 6  
Old 02-21-2003
It didn't work when I made the change as you've suggestd. Still causing error handling to be performed. It is only a problem on those lines where I did the cut. If I compare the line as is (last line) then it always executes successfully. This is why I am not sure if it is something else. I am not sure why the trimmed lines would be a problem though because what I trimmed and what I compared the trimmed lines to are identical as far as I can tell...
# 7  
Old 02-21-2003
Maybe you could put echo $first, echo $second and echo $third after "else" in the code just to make sure the values are what you intend them to be..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in string comparison

guys , i am using inotify for monitoring one directory to check core file generation , my snippet of code is follows #!/bin/bash DIR=$1 inotifywait -q -e create -m $DIR | while read path events name; do if ]; then echo "Now I am going to do something with $name in directory $path."... (5 Replies)
Discussion started by: baker
5 Replies

2. Shell Programming and Scripting

awk Help - Comparison Operator problem

Hi, I've tried searching through the forum but I've drawn a blank so i'm going to post here. I'm developing a number of checks on a CSV file, trying to find if any are greater than a max limit. I'm testing it by running it from a command line. The file I'm testing has 8 records. When I... (3 Replies)
Discussion started by: Tmart
3 Replies

3. Shell Programming and Scripting

Problem with character by character reading

Hi friend, i have the following problem: when i am writting the below command on the command prompt , its working. while read -n 1 ch; do echo "$ch" ; echo "$ch" ; done<file_name.out. but when i am executing it after saving it in a ksh file, its not working. Please helppppppppp .. thankss... (18 Replies)
Discussion started by: neelmani
18 Replies

4. Shell Programming and Scripting

String comparison problem

Hi, can someone please help me!!! urgent! I have a strange issue here. I grep for 2 strings from a txt files and compare the string value. Though the string values are the same, they are compared as different values. Please help Case-1 -------- Here I grep for 2 different field values... (3 Replies)
Discussion started by: vani123
3 Replies

5. UNIX for Dummies Questions & Answers

character-by-character comparison of strings

This might be a dummy question, but is there a command in UNIX that compare two strings character-by-character and display the difference? ---------- Post updated at 11:25 AM ---------- Previous update was at 10:32 AM ---------- Or probably what I'm looking is how to break a string into... (3 Replies)
Discussion started by: Orbix
3 Replies

6. Shell Programming and Scripting

Uppercase/lowercase comparison of one character per line with awk??

Another frustrating scripting problem from a biologist trying to manipulate a file with several millions line. For each of the line I need to compare the uppercase A or C or G or T with the lowercase a or c or g or t. If there are more uppercases, a + should be added to a new column, otherwise a -... (10 Replies)
Discussion started by: ivpz
10 Replies

7. Shell Programming and Scripting

problem in string comparison in shell programming

Hello, was just wondering how to compare strings in unix? I mean as in C there is a function strcmp() in string.h, is there any function in unix for that? I tried using if and all such variations but didn't succeed. Any help would be appreciated. Thanks in advance :) (9 Replies)
Discussion started by: salman4u
9 Replies

8. AIX

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (1 Reply)
Discussion started by: amarnath
1 Replies

9. Shell Programming and Scripting

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (3 Replies)
Discussion started by: amarnath
3 Replies

10. Shell Programming and Scripting

comparison problem

Hi, Is there any limitation on the no of characters to be compared using the if statement in Unix. We had an issue while comparing the following two nos. var1=20051031222900 & var2=20051101003545. The last six dgits are the time stamp for a day. The if statement is like this: if then move... (8 Replies)
Discussion started by: ranj@chn
8 Replies
Login or Register to Ask a Question