awk script throws invalid char expression

Hello there,

I am new to the awk scripting and getting the following error while running the script. Please can you advise me how to resolve this . Thanks
 ./sub_del_awk_lat.sh
awk: cmd. line:5: warning: escape sequence `\/' treated as plain `/'
awk: cmd. line:5: sed -n "/<sn:SuMSubscriberProfile/,/<\/sn:SuMSubscriberProfile/p" touch.xml > temp.xml
awk: cmd. line:5:                                                                        ^ syntax error
awk: cmd. line:5: sed -n "/<sn:SuMSubscriberProfile/,/<\/sn:SuMSubscriberProfile/p" touch.xml > temp.xml
awk: cmd. line:5:                                                                                   ^ syntax error
awk: cmd. line:6: value1=`cat temp.xml | grep $match_pattern2 | awk -F">|</" {
awk: cmd. line:6:        ^ invalid char '`' in expression

Script details are as follows:

awk 'match($0, /<sn:SuMSubscriberProfile/) {
match_pattern1="sn:MainSNwithBearerService"
match_pattern2="sn:accessRestrictionData"
match_pattern3="<sn:SuMSubscriberProfile"
sed -n "/<sn:SuMSubscriberProfile/,/<\/sn:SuMSubscriberProfile/p" touch.xml > temp.xml
value1=`cat temp.xml | grep $match_pattern2 | awk -F">|</" '{ print $2 }'`
if [ "value1" = "NORES" ]
then
value2=`cat temp.xml | grep $match_pattern3 | awk -F"\"" '{ print $2 }'`
value=`cat temp.xml | grep $match_pattern1 | awk -F">|</" '{ print $2 }'`
echo $value1
output="$value2 $value1 $value"
echo $output >> file
fi
}' touch.xml

AWK is an interpreted programming language that consists of series of rules or pattern and actions to perform upon the rules or pattern are matched:

awk ' 
pattern { action }
pattern { action }
...
' filename

So whatever actions you put has to be a valid AWK feature. I mean it should be something supported by your AWK program.

Check AWK man pages to understand what I'm talking about.

man awk

The reason why I'm telling this is because I see you have embedded sed and shell scripts inside your AWK program block which totally doesn't make any sense!

I recommend you to do some reading: GNU AWK User's Guide

List what input data you have, how you output should be and how to get the output.

The input file contains the data as follows:

<sn:data1="446776766">
<attributes>

     <sn:accessData>MAIL<\sn:accessData>
     ..........
     ...........
     ...........
     <sn:List_sn:Service>
       <sn:Service>4444-555<sn:Service>
       <sn:Service>4444-323<sn:Service>
    <\sn:List_sn:Service>
    .......................
    .....................
    .....................

<\attributes>

<\sn:data1>
<sn:data1="446776768">
<attributes>

     <sn:accessData>MAIL1<\sn:accessData>
     ..........
     ...........
     ...........
     <sn:List_sn:Service>
       <sn:Service>4443-555<sn:Service>
       <sn:Service>4443-323<sn:Service>
    <\sn:List_sn:Service>
    .......................
    .....................
    .....................

<\attributes>
<\sn:data1>

<sn:data1="446776796">
<attributes>

     <sn:accessData>MAIL2<\sn:accessData>
     ..........
     ...........
     ...........
     <sn:List_sn:Service>
       <sn:Service>4232-555<sn:Service>
       <sn:Service>3434-323<sn:Service>
    <\sn:List_sn:Service>
    .......................
    .....................
    .....................
<\attributes>
<\sn:data1>
.........
.........
    
.............
............
...........
...........
..............
.
............
...........
.

..............
.
<sn:data1="336776454">
<attributes>

     <sn:accessData>MAIL3<\sn:accessData>
     ..........
     ...........
     ...........
     <sn:List_sn:Service>
       <sn:Service>4232-8989<sn:Service>
       <sn:Service>3434-354646<sn:Service>
    <\sn:List_sn:Service>
    .......................
    .....................
    .....................
<\attributes>
<\sn:data1>

The script has to read this input file and display the data from the following elements : data1, accessData & Service in an output file in the following format.

446776766 MAIL  4444-555 4444-555
446776768 MAIL1 4443-555 4443-323
.........
.........

336776454 NORES 4232-8989 4232-8989

The input file is a huge file and using normal script takes lot of time to read the data. Just thought using awk help achieve to read the input data in less time.

This awk program might work for the XML data that you posted:

awk -F'[<>]' '
                /<sn:data1/ {
                                d = $0
                                gsub (/.*="|".*/, X, d)
                }
                /<sn:accessData/ {
                                m = $3
                }
                /<sn:Service/ {
                                s = s ? s " " $3 : $3
                }
                /<\\sn:data1/ {
                                print d, m, s
                                d = m = s = ""
                }
' file.xml
1 Like

Thanks for the awk code but this part of the code doesn't print any data.. Any idea what is causing this issue ? May be the variables are out of scope ?

/<\\sn:data1/ {
                                print d, m, s
                                d = m = s = ""
                }

In awk there is no way to make a variable local to a block, but you can make a variable local to a function.

Here is what I get:

$ cat file.xml
<sn:data1="446776766">
<attributes>
     <sn:accessData>MAIL<\sn:accessData>
     <sn:List_sn:Service>
       <sn:Service>4444-555<sn:Service>
       <sn:Service>4444-323<sn:Service>
    <\sn:List_sn:Service>
<\attributes>
<\sn:data1>
<sn:data1="446776768">
<attributes>
     <sn:accessData>MAIL1<\sn:accessData>
     <sn:List_sn:Service>
       <sn:Service>4443-555<sn:Service>
       <sn:Service>4443-323<sn:Service>
    <\sn:List_sn:Service>
<\attributes>
<\sn:data1>
$ awk -F'[<>]' '
                /<sn:data1/ {
                                d = $0
                                gsub (/.*="|".*/, X, d)
                }
                /<sn:accessData/ {
                                m = $3
                }
                /<sn:Service/ {
                                s = s ? s " " $3 : $3
                }
                /<\\sn:data1/ {
                                print d, m, s
                                d = m = s = ""
                }
' file.xml
446776766 MAIL 4444-555 4444-323
446776768 MAIL1 4443-555 4443-323