Execution Problems

this my source file

************* fixed  ***************
Begin  

equipmentId : d9
processor : fox
number : bhhhhhh


Variable # 1: 
  
Id           : 100
Type         : 9
nType        : s
gType        : 5f
mType        : 4
LField       : England
DataField    : london
Length       : 4

 End 


Variable # 2: 
  
Id           : 200
Type         : 9
nType        : f
gType        : f
mType        : 9
LField       : England
DataField    : london
Length       : 4

 End 


Variable # 3: 
  
Id           : 400
Type         : 7
nType        : c
gType        : r
mType        : h
LField       : Egypt
DataField    : Cairo
Length       : 4

 End 

Variable # 4: 
  
Id           : 515
Type         : 7
nType        : 1
gType        : 5d
mType        : 9a
LField       : France
DataField    : Paris
Length       : 2

 End 


End of  section.

************* fixed  ***************

Begin 

equipmentId : d9
processor : fox
number : bhhhhhh


Variable # 1: 
  
Id           : 515
Type         : 9
nType        : s
gType        : 5f
mType        : 4
LField       : England
DataField    : london
Length       : 4

 End 

Variable # 2: 
  
Id           : 550
Type         : 9
nType        : f
gType        : f
mType        : 9
LField       : France
DataField    : Paris
Length       : 4

 End


Variable # 3: 
  
Id           : 600
Type         : 7
nType        : c
gType        : r
mType        : h
LField       : Egypt
DataField    : Cairo
Length       : 4

 End  
  
End of  section.

************* fixed  ***************

Begin  

equipmentId : d9
processor : fox
number : bhhhhhh


Variable # 1: 
  
Id           : 100
Type         : 9
nType        : s
gType        : 5f
mType        : 4
LField       : England
DataField    : london
Length       : 4

 End 

Variable # 2: 
  
Id           : 516
Type         : 9
nType        : f
gType        : f
mType        : 9
LField       : Egypt
DataField    : Cairo
Length       : 4

 End 

Variable # 3: 
  
Id           : 609
Type         : 7
nType        : c
gType        : r
mType        : h
LField       : usa
DataField    : NY
Length       : 4

 End
  

Variable # 4: 
  
Id           : 715
Type         : 7
nType        : 1
gType        : 5d
mType        : 9a
LField       : germany
DataField    : munich
Length       : 2

 End 

End of  section.
************* fixed  ***************

Begin  

equipmentId : d9
processor : fox
number : bhhhhhh


Variable # 1: 
  
Id           : 100
Type         : 9
nType        : s
gType        : 5f
mType        : 4
LField       : England
DataField    : london
Length       : 4

 End 

End of section.

i`ve asked get a column getting ( DataField under ID : 515 or 516 ) in the variable part that its not in same place every time and variable fields are not of a fixed number and if he couldnt find ID 515 nor 516 return with ( not found )
thats mean our output

Paris 
london 
cairo 
not found   ....... as Id 515 not found nor 516 

Chubler_xl got me this

nawk -v target=${1:-515} '
    $1 == "End" && $2 == "of" {
        if( value["Id"] == target )
            print value["DataField"];
        else print "Not Found";
        split( "", value, "." );
    }
    $1 == "End" {snarf=0; next }
    $1 == "Id" && $3 == target { snarf=1 }
    snarf && $2 == ":" {value[$1] = $3}
' infile

this gets mee only id = 515 what if [ 516 and 515 ]
i tried to make it but nothing done please help as fast as possible :smiley:

So it needs to find both in the same section, or else print 'not found'?

Or is it okay if the section only contains some of them?

no the section will contain either one of them ( either 516 or 515 ) not both in same section
if finds one of them get the data field of it in both one column if not prints " not found "

How about this:

awk -v target=${1:-515|516} '
    $1 == "End" && $2 == "of" {
        if( match(value["Id"], target))
            print value["DataField"];
                else print "Not Found";
                split( "", value, "." );
    }
    $1 == "End" {snarf=0; next }
        $1 == "Id" && $3 == target { snarf=1 }
    snarf && $2 == ":" {value[$1] = $3}
' infile

thanks for your reply
1) it doesnt work with (awk) it will work with (nawk)

2) when i do it with nawk the output all is not found

can u handle it again

Sorry should test before I post, this is what you want:

awk -v target="${1:-515|516}" '
    $1 == "End" && $2 == "of" {
        if( value["Id"] ~ target )
            print value["DataField"];
                else print "Not Found";
                split( "", value, "." );
    }
    $1 == "End" {snarf=0; next }
        $1 == "Id" && $3 ~ target { snarf=1 }
    snarf && $2 == ":" {value[$1] = $3}
' infile
1 Like

thanks alot :smiley:
merci