[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1900
  • Last Modified:

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??
0
rcsorber
Asked:
rcsorber
  • 2
  • 2
1 Solution
 
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
 
rcsorberAuthor Commented:
thank you for the help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now