Need Help for Adding Three new columns in existing file from fatching data from file

not required this time

Please, give us examples of input and output files.

Jean-Pierre.

Not required this time

A possible solution (try and adapt) :

awk '

#
# DCDB
#

/^<DCDBEntry / {
   split($0, dcdb, /"/);
   sub(/ *$/, "", dcdb[2]);
   Folder[dcdb[2]] = dcdb[4];
       Tz[dcdb[2]] = dcdb[6];
   next;
}

#
# LDS
#

/^<LDSEntry / {
   split($0, lds, /[":]/);
   folder = lds[2];
   tz     = lds[6];
   SiteUnit[folder, tz] = lds[3];
     Device[folder, tz] = lds[4];
   next;
}

#
# Input
#

/^</ {
   next;
}

FNR==1 {
   print $0, "Folder", "SU", "Dev";
   next;
}

{
   dcdb_in = $2;
   folder = Folder[dcdb_in];
   tz     =     Tz[dcdb_in];
   su     = SiteUnit[folder, tz];
   dev    =   Device[folder, tz];
   print $0, (folder ? folder : "?"), (su ? su : "?"), (dev ? dev : "?");
   next;
}

' DCDB.xml LDS-*.xml inputfile

DCDB.xml

> cat DCDB.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DCDBTable>
<DCDBEntry DCDB="0862976 " folder="768678" timeZone="2"/>
<DCDBEntry DCDB="0911297 " folder="975426" timeZone="1"/>
<DCDBEntry DCDB="0201347 " folder="1389781" timeZone="2"/>
<DCDBEntry DCDB="0800659 " folder="2035595" timeZone="4"/>
<DCDBEntry DCDB="0123033 " folder="2143699" timeZone="2"/>
<DCDBEntry DCDB="0911515 " folder="2315643" timeZone="1"/>
<DCDBEntry DCDB="0123913 " folder="2367867" timeZone="2"/>
<DCDBEntry DCDB="0713934 " folder="2407712" timeZone="5"/>
</DCDBTable>

LDS-*.xml

> cat LDS-*.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LDSMappingTable>
<LDSEntry LDSKey="3351216:1:TV" LDSValue="7"/>
<LDSEntry LDSKey="1389781:1:VCR1" LDSValue="2"/>
<LDSEntry LDSKey="3351216:2:TV" LDSValue="2"/>
<LDSEntry LDSKey="3351216:3:TV" LDSValue="6"/>
<LDSEntry LDSKey="3351511:1:TV" LDSValue="10"/>
<LDSEntry LDSKey="3351511:1:VCR1" LDSValue="11"/>
<LDSEntry LDSKey="3351511:2:TV" LDSValue="4"/>
<LDSEntry LDSKey="3351511:2:VCR1" LDSValue="5"/>
</LDSMappingTable>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LDSMappingTable>
<LDSEntry LDSKey="3351217:1:XTV" LDSValue="7"/>
<LDSEntry LDSKey="1389782:1:XVCR1" LDSValue="2"/>
<LDSEntry LDSKey="3351217:2:XTV" LDSValue="2"/>
<LDSEntry LDSKey="3351217:3:XTV" LDSValue="6"/>
<LDSEntry LDSKey="3351512:1:XTV" LDSValue="10"/>
<LDSEntry LDSKey="3351512:1:XVCR1" LDSValue="11"/>
<LDSEntry LDSKey="3351512:2:XTV" LDSValue="4"/>
<LDSEntry LDSKey="3351512:2:XVCR1" LDSValue="5"/>
</LDSMappingTable>

Inputfile

> cat inputfile
1DATE HHIDLDS BothOff 91 NG A=D 90faulRoundOnOff OffOn OthersMATCHED NOMATCH MATCH%
0721 0201136 1 544 . . . 1 . . . 895 1 99.89
0721 0201347 1 1296 . . . . . . . 144 . 100.0
0721 0201347 2 818 . . . . . . . 622 . 100.0
0721 0201364 1 1123 . . . . . . . 317 . 100.0
0721 0201364 2 1327 . . . . . . . 113 . 100.0

Output

1DATE HHIDLDS BothOff 91 NG A=D 90faulRoundOnOff OffOn OthersMATCHED NOMATCH MATCH% Folder SU Dev
0721 0201136 1 544 . . . 1 . . . 895 1 99.89 ? ? ?
0721 0201347 1 1296 . . . . . . . 144 . 100.0 1389781 1 VCR1
0721 0201347 2 818 . . . . . . . 622 . 100.0 1389781 1 VCR1
0721 0201364 1 1123 . . . . . . . 317 . 100.0 ? ? ?
0721 0201364 2 1327 . . . . . . . 113 . 100.0 ? ? ?

Jean-Pierre.

Thanks a lot Jean-Pierre
but still i have facing some error syntax like as:
awk: syntax error near line 1
awk: bailing out near line 1
awk: newline in string near line 8
awk: newline in string near line 20

Please help me

Try nawk or gawk instead of awk.

Jean-Pierre.

Thanks Jean for prompt reply
I'll try with nawk and share my experiance with you

Not required this time

Seems that there is a missing quote ... The awk command is in the form :

awk '

. . . .

' DCDB.xml LDS-*.xml inputfile

Jean-Pierre.

I am running this script as:
nawk -f test.awk >new.txt

it gives error
nawk: syntax error at source line 1
context is
awk >>> ' <<<
nawk: bailing out at source line 1

Please suggest me, how to run this script with nawk ?

Your test.awk program must not contain quotes and file names.
awk program test.awk :

#
# DCDB
#

/^<DCDBEntry / {
   split($0, dcdb, /"/);
   sub(/ *$/, "", dcdb[2]);
   Folder[dcdb[2]] = dcdb[4];
       Tz[dcdb[2]] = dcdb[6];
   next;
}

#
# LDS
#

/^<LDSEntry / {
   split($0, lds, /[":]/);
   folder = lds[2];
   tz     = lds[6];
   SiteUnit[folder, tz] = lds[3];
     Device[folder, tz] = lds[4];
   next;
}

#
# Input
#

/^</ {
   next;
}

FNR==1 {
   print $0, "Folder", "SU", "Dev";
   next;
}

{
   dcdb_in = $2;
   folder = Folder[dcdb_in];
   tz     =     Tz[dcdb_in];
   su     = SiteUnit[folder, tz];
   dev    =   Device[folder, tz];
   print $0, (folder ? folder : "?"), (su ? su : "?"), (dev ? dev : "?");
   next;
}

Running awk program:

nawk -f test.awk DCDB.xml LDS-*.xml inputfile >new.txt

Jean-Pierre.

Not required this time

Try the follwing version of the awk program, it include debug displays :

FNR==1 {
   print "\nDbg --",FILENAME,"-------------------------"
}

#
# DCDB
#

/^<DCDBEntry / {
   printf("Dbg Record #%d: %s\n", FNR,$0);
   split($0, dcdb, /"/);
   sub(/ *$/, "", dcdb[2]);
   Folder[dcdb[2]] = dcdb[4];
       Tz[dcdb[2]] = dcdb[6];
   printf("Dbg DCDB <%s> Folder <%s> Tz <%s>\n", dcdb[2], Folder[dcdb[2]], Tz[dcdb[2]]);
   next;
}

#
# LDS
#

/^<LDSEntry / {
   printf("Dbg Record #%d:%s\n", FNR,$0);
   split($0, lds, /[":]/);
   folder = lds[2];
   tz     = lds[6];
   SiteUnit[folder, tz] = lds[3];
     Device[folder, tz] = lds[4];
   printf("Dbg LDS <%s>,<%s> Site <%s> Dev <%s>\n", folder, tz, SiteUnit[folder, tz], Device[folder, tz]);
   next;
}

#
# Input
#

/^</ {
   next;
}

FNR==1 {
   print $0, "Folder", "SU", "Dev";
   next;
}

{
   printf("Dbg Record #d:%s\n", FNR,$0);
   dcdb_in = $2;
   folder = Folder[dcdb_in];
   tz     =     Tz[dcdb_in];
   su     = SiteUnit[folder, tz];
   dev    =   Device[folder, tz];
   print $0, (folder ? folder : "?"), (su ? su : "?"), (dev ? dev : "?");
   next;
}

Output (whith sames files than in my previus post) :

Dbg -- DCDB.xml -------------------------
Dbg Record #3: <DCDBEntry DCDB="0862976 " folder="768678" timeZone="2"/>
Dbg DCDB <0862976> Folder <768678> Tz <2>
Dbg Record #4: <DCDBEntry DCDB="0911297 " folder="975426" timeZone="1"/>
Dbg DCDB <0911297> Folder <975426> Tz <1>
Dbg Record #5: <DCDBEntry DCDB="0201347 " folder="1389781" timeZone="2"/>
Dbg DCDB <0201347> Folder <1389781> Tz <2>
Dbg Record #6: <DCDBEntry DCDB="0800659 " folder="2035595" timeZone="4"/>
Dbg DCDB <0800659> Folder <2035595> Tz <4>
Dbg Record #7: <DCDBEntry DCDB="0123033 " folder="2143699" timeZone="2"/>
Dbg DCDB <0123033> Folder <2143699> Tz <2>
Dbg Record #8: <DCDBEntry DCDB="0911515 " folder="2315643" timeZone="1"/>
Dbg DCDB <0911515> Folder <2315643> Tz <1>
Dbg Record #9: <DCDBEntry DCDB="0123913 " folder="2367867" timeZone="2"/>
Dbg DCDB <0123913> Folder <2367867> Tz <2>
Dbg Record #10: <DCDBEntry DCDB="0713934 " folder="2407712" timeZone="5"/>
Dbg DCDB <0713934> Folder <2407712> Tz <5>

Dbg -- LDS-1.xml -------------------------
Dbg Record #3: <LDSEntry LDSKey="3351216:1:TV" LDSValue="7"/>
Dbg LDS <3351216>,<7> Site <1> Dev <TV>
Dbg Record #4: <LDSEntry LDSKey="1389781:1:VCR1" LDSValue="2"/>
Dbg LDS <1389781>,<2> Site <1> Dev <VCR1>
Dbg Record #5: <LDSEntry LDSKey="3351216:2:TV" LDSValue="2"/>
Dbg LDS <3351216>,<2> Site <2> Dev <TV>
Dbg Record #6: <LDSEntry LDSKey="3351216:3:TV" LDSValue="6"/>
Dbg LDS <3351216>,<6> Site <3> Dev <TV>
Dbg Record #7: <LDSEntry LDSKey="3351511:1:TV" LDSValue="10"/>
Dbg LDS <3351511>,<10> Site <1> Dev <TV>
Dbg Record #8: <LDSEntry LDSKey="3351511:1:VCR1" LDSValue="11"/>
Dbg LDS <3351511>,<11> Site <1> Dev <VCR1>
Dbg Record #9: <LDSEntry LDSKey="3351511:2:TV" LDSValue="4"/>
Dbg LDS <3351511>,<4> Site <2> Dev <TV>
Dbg Record #10: <LDSEntry LDSKey="3351511:2:VCR1" LDSValue="5"/>
Dbg LDS <3351511>,<5> Site <2> Dev <VCR1>

Dbg -- LDS-2.xml -------------------------
Dbg Record #3: <LDSEntry LDSKey="3351217:1:XTV" LDSValue="7"/>
Dbg LDS <3351217>,<7> Site <1> Dev <XTV>
Dbg Record #4: <LDSEntry LDSKey="1389782:1:XVCR1" LDSValue="2"/>
Dbg LDS <1389782>,<2> Site <1> Dev <XVCR1>
Dbg Record #5: <LDSEntry LDSKey="3351217:2:XTV" LDSValue="2"/>
Dbg LDS <3351217>,<2> Site <2> Dev <XTV>
Dbg Record #6: <LDSEntry LDSKey="3351217:3:XTV" LDSValue="6"/>
Dbg LDS <3351217>,<6> Site <3> Dev <XTV>
Dbg Record #7: <LDSEntry LDSKey="3351512:1:XTV" LDSValue="10"/>
Dbg LDS <3351512>,<10> Site <1> Dev <XTV>
Dbg Record #8: <LDSEntry LDSKey="3351512:1:XVCR1" LDSValue="11"/>
Dbg LDS <3351512>,<11> Site <1> Dev <XVCR1>
Dbg Record #9: <LDSEntry LDSKey="3351512:2:XTV" LDSValue="4"/>
Dbg LDS <3351512>,<4> Site <2> Dev <XTV>
Dbg Record #10: <LDSEntry LDSKey="3351512:2:XVCR1" LDSValue="5"/>
Dbg LDS <3351512>,<5> Site <2> Dev <XVCR1>

Dbg -- inputfile -------------------------
1DATE HHIDLDS BothOff 91 NG A=D 90faulRoundOnOff OffOn OthersMATCHED NOMATCH MATCH% Folder SU Dev
Dbg Record #d:2
0721 0201136 1 544 . . . 1 . . . 895 1 99.89 ? ? ?
Dbg Record #d:3
0721 0201347 1 1296 . . . . . . . 144 . 100.0 1389781 1 VCR1
Dbg Record #d:4
0721 0201347 2 818 . . . . . . . 622 . 100.0 1389781 1 VCR1
Dbg Record #d:5
0721 0201364 1 1123 . . . . . . . 317 . 100.0 ? ? ?
Dbg Record #d:6
0721 0201364 2 1327 . . . . . . . 113 . 100.0 ? ? ?

Run the awk program and post the output.

Jean-Pierre.

Thanks Jean for your quick solution.
I'll write back after using above suggestion

Please provide line by line understanding of this script, that really help me.

#
# DCDB - This block select all DCBEntry records
# and memorize Folder and TZ.
# => Folder[dcdb] and Tz[dcdb]
#

/^<DCDBEntry / {                    # Select DCDBEntry records
   split($0, dcdb, /"/);            #    Split record into dcdb array (sep=")
                                    #      dcdb[2]=dcdb, dcdb[4]=folder, 
                                    #      dcdb[6]=tz
   sub(/ *$/, "", dcdb[2]);         #    Remove trailing spaces from dcdb
   Folder[dcdb[2]] = dcdb[4];       #    Memorize folder for dcdb
       Tz[dcdb[2]] = dcdb[6];       #    Memorize tz for dcdb
   next;                            #    Proceed next input record
}                                   #

#
# LDS - This block select all LDSEntry records
# and memorize SiteUnit and Devide for folder,tz
# => SiteUnit[folder,tz] and Devicefolder,tz
#

/^<LDSEntry / {                     # Select LDSEntry records
   split($0, lds, /[":]/);          #    Split record into lds array (sep=" or sep=:)
                                    #      lds[2]=folder, lds[3]=siteunit
                                    #      lds[4]=device, lds[6]=tz
   folder = lds[2];                 #    Get folder
   tz     = lds[6];                 #    Get tz
   SiteUnit[folder, tz] = lds[3];   #    Memorize siteunit for folder,tz
     Device[folder, tz] = lds[4];   #    Memorize device for folder,tz
   next;                            #    Proceed next input record
}                                   #

#
# Input - These blocks proceed inputfiles
#

/^</ {                              # Select (and ignore) records from DCDB and LDS 
                                    # (DCDBEntry ans LDSEntry have already been processed)
   next;                            #    Procced next input record
}                                   #

FNR==1 {                            # Select first record of inputfile (logically all other files
                                    # have been processed in previus blocks).
   print $0, "Folder", "SU", "Dev"; #    Print first record (headers) and add new headers
   next;                            #    Proceed next input record
}                                   #

{                                   # Select all reamaining inputfile records
   dcdb_in = $2;                    #    Get dcdb from input record
   folder = Folder[dcdb_in];        #    Get memorized folder for this dcdb
   tz     =     Tz[dcdb_in];        #    Get memorized tz  for this dcdb
   su     = SiteUnit[folder, tz];   #    Get memorized siteunit for this forder,tz
   dev    =   Device[folder, tz];   #    Get memorized device for this forder,tz
   print $0, (folder ? folder : "?"), (su ? su : "?"), (dev ? dev : "?"); # Print record,
                                    #    folder, siteunit and device (unknown values are
                                    #    replaced by ?
   next;                            #    Proceed next input record
}                                   #

Jean-Pierre.

Thanks a lot Jean

Not required this time

Are you sure that all the records start with '<' in the files DCDB.xml and LDS.xml ?
I have tested the commented awk script without problem :

> cat DCDB.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DCDBTable>
<DCDBEntry DCDB="0862976 " folder="768678" timeZone="2"/>
<DCDBEntry DCDB="0911297 " folder="975426" timeZone="1"/>
<DCDBEntry DCDB="0201347 " folder="1389781" timeZone="2"/>
<DCDBEntry DCDB="0800659 " folder="2035595" timeZone="4"/>
<DCDBEntry DCDB="0123033 " folder="2143699" timeZone="2"/>
<DCDBEntry DCDB="0911515 " folder="2315643" timeZone="1"/>
<DCDBEntry DCDB="0123913 " folder="2367867" timeZone="2"/>
<DCDBEntry DCDB="0713934 " folder="2407712" timeZone="5"/>
</DCDBTable>
> cat LDS-1.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LDSMappingTable>
<LDSEntry LDSKey="3351216:1:TV" LDSValue="7"/>
<LDSEntry LDSKey="1389781:1:VCR1" LDSValue="2"/>
<LDSEntry LDSKey="3351216:2:TV" LDSValue="2"/>
<LDSEntry LDSKey="3351216:3:TV" LDSValue="6"/>
<LDSEntry LDSKey="3351511:1:TV" LDSValue="10"/>
<LDSEntry LDSKey="3351511:1:VCR1" LDSValue="11"/>
<LDSEntry LDSKey="3351511:2:TV" LDSValue="4"/>
<LDSEntry LDSKey="3351511:2:VCR1" LDSValue="5"/>
</LDSMappingTable>
> cat LDS-2.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LDSMappingTable>
<LDSEntry LDSKey="3351217:1:XTV" LDSValue="7"/>
<LDSEntry LDSKey="1389782:1:XVCR1" LDSValue="2"/>
<LDSEntry LDSKey="3351217:2:XTV" LDSValue="2"/>
<LDSEntry LDSKey="3351217:3:XTV" LDSValue="6"/>
<LDSEntry LDSKey="3351512:1:XTV" LDSValue="10"/>
<LDSEntry LDSKey="3351512:1:XVCR1" LDSValue="11"/>
<LDSEntry LDSKey="3351512:2:XTV" LDSValue="4"/>
<LDSEntry LDSKey="3351512:2:XVCR1" LDSValue="5"/>
</LDSMappingTable>
> cat inputfile
1DATE HHIDLDS BothOff 91 NG A=D 90faulRoundOnOff OffOn OthersMATCHED NOMATCH MATCH%
0721 0201136 1 544 . . . 1 . . . 895 1 99.89
0721 0201347 1 1296 . . . . . . . 144 . 100.0
0721 0201347 2 818 . . . . . . . 622 . 100.0
0721 0201364 1 1123 . . . . . . . 317 . 100.0
0721 0201364 2 1327 . . . . . . . 113 . 100.0
> cat Malik.awk
#
# DCDB - This block select all DCBEntry records
# and memorize Folder and TZ.
# => Folder[dcdb] and Tz[dcdb]
#

/^<DCDBEntry / {                    # Select DCDBEntry records
   split($0, dcdb, /"/);            #    Split record into dcdb array (sep=")
                                    #      dcdb[2]=dcdb, dcdb[4]=folder,
                                    #      dcdb[6]=tz
   sub(/ *$/, "", dcdb[2]);         #    Remove trailing spaces from dcdb
   Folder[dcdb[2]] = dcdb[4];       #    Memorize folder for dcdb
       Tz[dcdb[2]] = dcdb[6];       #    Memorize tz for dcdb
   next;                            #    Proceed next input record
}                                   #

#
# LDS - This block select all LDSEntry records
# and memorize SiteUnit and Devide for folder,tz
# => SiteUnit[folder,tz] and Devicefolder,tz
#

/^<LDSEntry / {                     # Select LDSEntry records
   split($0, lds, /[":]/);          #    Split record into lds array (sep=" or sep=:)
                                    #      lds[2]=folder, lds[3]=siteunit
                                    #      lds[4]=device, lds[6]=tz
   folder = lds[2];                 #    Get folder
   tz     = lds[6];                 #    Get tz
   SiteUnit[folder, tz] = lds[3];   #    Memorize siteunit for folder,tz
     Device[folder, tz] = lds[4];   #    Memorize device for folder,tz
   next;                            #    Proceed next input record
}                                   #

#
# Input - These blocks proceed inputfiles
#

/^</ {                              # Select (and ignore) records from DCDB and LDS
                                    # (DCDBEntry ans LDSEntry have already been processed)
   next;                            #    Procced next input record
}                                   #

FNR==1 {                            # Select first record of inputfile (logically all other files
                                    # have been processed in previus blocks).
   print $0, "Folder", "SU", "Dev"; #    Print first record (headers) and add new headers
   next;                            #    Proceed next input record
}                                   #

{                                   # Select all reamaining inputfile records
   dcdb_in = $2;                    #    Get dcdb from input record
   folder = Folder[dcdb_in];        #    Get memorized folder for this dcdb
   tz     =     Tz[dcdb_in];        #    Get memorized tz  for this dcdb
   su     = SiteUnit[folder, tz];   #    Get memorized siteunit for this forder,tz
   dev    =   Device[folder, tz];   #    Get memorized device for this forder,tz
   print $0, (folder ? folder : "?"), (su ? su : "?"), (dev ? dev : "?"); # Print record,
                                    #    folder, siteunit and device (unknown values are
                                    #    replaced by ?
   next;                            #    Proceed next input record
}
> awk -f Malik.awk DCDB.xml LDS-1.xml LDS-2.xml inputfile
1DATE HHIDLDS BothOff 91 NG A=D 90faulRoundOnOff OffOn OthersMATCHED NOMATCH MATCH% Folder SU Dev
0721 0201136 1 544 . . . 1 . . . 895 1 99.89 ? ? ?
0721 0201347 1 1296 . . . . . . . 144 . 100.0 1389781 1 VCR1
0721 0201347 2 818 . . . . . . . 622 . 100.0 1389781 1 VCR1
0721 0201364 1 1123 . . . . . . . 317 . 100.0 ? ? ?
0721 0201364 2 1327 . . . . . . . 113 . 100.0 ? ? ?
>

Jean-Pierre.

Not required this time