Need help with string search in relative to first string found. 
Author Message
 Need help with string search in relative to first string found.

I am having problem to find a string relative to another string.
Below is the detail.

Below is a file called abcd.txt
=======================================================

service:: catering
          food type: southwestern
          guests # : 123
          food type: american
          guests #: 200
          childrens #: 35
          food type : mexican
          guests #: 56
service:: ticketing
          destination: Acapulco
          tourist #: 67
          type of transportation : Air
          destination : Bahamas
          tourists #: 90
          children #:20
==========================================================

Basically I can search for the string "service:: catering:, now once I
find the search string, then I need to find the string "food type:". If
you notice the string "food type:" is in relation to the 1st string search
of "service:: catering". My question How do I tell the awk program to move
the file pointer from the 1st string search.

Belwo is an awk program I wrote, but this will find the string relative to
1st string search, but the fields has to be same. But in my case above the
fields are not same always. This is where my dilema is ?

Myawk program:
===================================================
#!/bin/ksh
awk -f abcd.txt ' \
BEGIN{RS="\n";FS="\n"}
if $1 ~ (service:: catering)
print ($2)
=====================================================

If you notice in the above awk program, I knew the  food type to be
$2, but the food type is not in order. It can be in any field relative
to the string "service:: catering".

I would very much appreciate any help. This has been boggling my mind.
I prefer email replies.

Thanks
Masroor Ahmed



Wed, 29 Mar 2000 03:00:00 GMT  
 Need help with string search in relative to first string found.

% I am having problem to find a string relative to another string.
% Below is the detail.
%
% Below is a file called abcd.txt
% =======================================================
%
% service:: catering
%           food type: southwestern
%           guests # : 123
%           food type: american
%           guests #: 200
%           childrens #: 35
%           food type : mexican
%           guests #: 56
% service:: ticketing
%           destination: Acapulco
%           tourist #: 67
%           type of transportation : Air
%           destination : Bahamas
%           tourists #: 90
%           children #:20

[...]

Use a state variable to tell what service you're dealing with. For instance:

 # make : surrounded by arbitrary blanks and tabs the field separator,
 # and initialise the state variable
 BEGIN { FS="[ \t]*:[ \t]*"; service = "" }
 $1 == "service" && $2 == "" { service = $3 }
 # do whatever you like -- I don't care, so I'll just print the food type
 service == "catering" && $1 ~ "[ \t]*food type" { print $2 }

hope that helps.
--

Patrick TJ McPhee
East York  Canada



Thu, 30 Mar 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. regexp and string first woes, help needed.

2. need help substituting shell variable into search string with awk

3. First X Words in a String can't use [string wordend] nor [lrange]

4. : string first and string last too slow in 8.0

5. string first and string last too slow in 8.0

6. Finding first instance of string in multiple files

7. Search a string in a string

8. Searching for a string inside a string

9. How to find string in string

10. Help: Replacing the first occurrence of a string in a file

11. Ada String Issue: String within Strings

12. string = string(i:j) // string(k:n)

 

 
Powered by phpBB® Forum Software