Fields with no separation Chars 
Author Message
 Fields with no separation Chars

Quote:

> I am trying to write a program that would go through a telephone
> directory and give me the results of the search. The thing is, the
> fields are not separated by anything in particular, though they do
> have a start and end column. Also to make it a bit more complicated
> there are two records per line. The way I see the algorthm is:

>   Does line contain search string?
>     If it does does it occur before or after char number x
>       If it is before then
>         Name = $1 -- $1 starts at char s1 and end at char e1
>         Department = $2 -- $2 starts at char s2 and end at char e2
>         Ext = $3
>       If it is after then
>         Name = $4
>         Department = $5
>         Ext = $6

Here is what I would do  (of course since I don't have access to
my unix box right now, this code is completely un-tested)

/Match-Search-String/ {

   firstHalf = substr($0, 0, x);
   secondHalf = substr($0, x, (totalLineLength - x));

   if (index(firstHalf, "searchString")) {
       hitCount ++;
       Name = substr(firstHalf, s1, (lengthfirstHalf - e1));
       Dept = substr(firstHalf, s2, (lengthfirstHalf - e2));
       Ext = substr(firstHalf, s3, (lengthfirstHalf - e3));
        }
   if (index(secondHalf, "searchString")) {
       hitCount ++;
       Name = substr(secondHalf, s1, (lengthsecondHalf - e1));
       Dept = substr(secondHalf, s2, (lengthsecondHalf - e2));
       Ext = substr(secondHalf, s3, (lengthsecondHalf - e3));
        }  

Quote:
}

--
| Matt Baney          Boeing Commercial Space Company
| Resouce21           (206)-393-0148                      
 "Whenever you find yourself at the bottom of a deep  
  hole the first thing you should do is stop digging"


Sat, 14 Aug 1999 03:00:00 GMT  
 Fields with no separation Chars

Quote:

>I am trying to write a program that would go through a telephone
>directory and give me the results of the search.

/pattern/ { #we have a line that matches, but which record on the line?

Quote:
}
>The thing is, the fields are not separated by anything in particular,
>though they do have a start and end column.

Not to worry. substr() will chop it up for you.

Quote:
>Also to make it a bit more complicated there are two records per
>line.

The only complications the ones that you seem to have overlooked: what
if the match straddles the boundary? What if there is more than one
match on the line?

One trick you can try is using the pattern as the field delimiter:

awk -Fpattern '.....' directory

NF will then immediately tell you how many matches there are.

Quote:
>  Does line contain search string?
>    If it does does it occur before or after char number x
>      If it is before then
>        Name = $1 -- $1 starts at char s1 and end at char e1
>        Department = $2 -- $2 starts at char s2 and end at char e2
>        Ext = $3
>      If it is after then
>        Name = $4
>        Department = $5
>        Ext = $6

One problem: I assume that the people in this directory have first and
last names.

Quote:
>On Unix I could probably do this using grep and cut, though I am a
>MSDOS and I can't see these functions around.

Microsoft policy: Only the basics. You PAY for the rest.

For a decade now, I've used the MKS Toolkit versions. (Money well
invested since they converted me to AWK.)

===

    |\^/|     Maynard Hogg
 _|\|   |/|_  #306, 4-30-10 Yoga, Setagaya-ku, Tokyo, Japan 158
 >         <  Fax: +81-3-3700-7399

Unsolicited commercial electronic mail sent to this address will be
proofread at a cost of US$200/hour (half-hour minimum).



Tue, 17 Aug 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Year 2000 and char fields

2. NULL Chars in Database Fields

3. length of char field

4. More than 254 char in a DBF field

5. Assign of Char varing field to array structure

6. Assign of Char varying field to array structure

7. Moving char data into and out of packed fields efficiently

8. limit char for entry fields

9. Tab char in text fields

10. Object comment do: [:char | char yourself]

11. reversing lines char by char, but not the line order in a file

12. How to read line char by char?

 

 
Powered by phpBB® Forum Software