AWK string comparison

In the following program, if $RECV = 9252610000,  the output line is written.  However if $RECV = 925261CORP then no output is written.


FILE UCSIN:
ISA*00*          *00*          *08*020694667      *01*925261CORP     *991220*1322*U*00307*000027408*0*P*>~


COMMAND LINE:

cat UCSIN | awk -f cutfile.awk -v receiver=$RECV  >> UCSOUT


CUTFILE.AWK:

BEGIN {
    FS="*"
   }

   {
     if ( ( $1 == "ISA" ) && ( $9 == receiver ) )
        {
         print $0
        }
   }

The problem is $9 = "925261CORP     " and receiver = "925261CORP"
How do you drop the trailing spaces in $9??
rcsorberAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
FS=" *[*]"
0
expert99121199Commented:
use:

if ( ( $1 == "ISA" ) && (match($9,receiver) == 1)

If the first part of $9 exactly matches receiver the line will be printed, so the spaces will not be significant.
0
rcsorberAuthor Commented:
expert99, thank you.  it is very close but still a problem.  If RECV = 925261CORP, and file UCSIN contains the record:
ISA*00*          *00*          *08*020694667      *01*925261CORPA    *991220*1322*U*00307*000027408*0*P*>~
The record is printed when it shouldn't be.
0
expert99121199Commented:
Sorry, my mistake.  I incorrectly assumed that the number of spaces would always be the same.  This code watches for no spaces, all spaces, and it verifies that the match of non-space characters is exact. Paste this in after the BEGIN section:

{
 
# first see if we have ISA

if ( $1 == "ISA" ) {

      # now it gets a bit more complicated
      # find the position of the first space
      # can be zero if there is no space

      FIRST_SPACE = index($9," ")

      if ( FIRST_SPACE == 0 ) {
            # no spaces in $9
            # look for exact match
            if ( $9 == receiver ) {
                  print $0
            }
      }
      else {
            # there is a space in $9 somewhere
      
            if ( FIRST_SPACE > 1 ) {
                  if ( substr($9,1,FIRST_SPACE - 1 ) == receiver ) {
                        print $0
                  }
            }
      }
}

# matches first brace above
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rcsorberAuthor Commented:
thank you for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.