awk script throws invalid char expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script throws invalid char expression
# 1  
Old 04-26-2013
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

Code:
 ./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:
=========================

Code:
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


Last edited by Corona688; 04-26-2013 at 05:35 PM..
# 2  
Old 04-26-2013
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:
Code:
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.
Code:
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
# 3  
Old 04-26-2013
List what input data you have, how you output should be and how to get the output.
# 4  
Old 04-26-2013
The input file contains the data as follows:

Code:
<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.


Code:
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.

Last edited by Scrutinizer; 04-27-2013 at 07:51 AM.. Reason: code tags
# 5  
Old 04-26-2013
This awk program might work for the XML data that you posted:
Code:
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

This User Gave Thanks to Yoda For This Post:
# 6  
Old 04-27-2013
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 ?

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


Last edited by Scrutinizer; 04-27-2013 at 07:51 AM.. Reason: code tags
# 7  
Old 04-27-2013
Quote:
Originally Posted by Sudhakar333
May be the variables are out of scope ?
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:
Code:
$ 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>

Code:
$ 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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed: -e expression #1, char 20: unterminated address regex

I am trying to add word in last of particular line. the same command syntex is running on prompt. but in bash script give error."sed: -e expression #1, char 20: unterminated address regex" Please help. for i in `cat servername`; do ssh -q -t root@$i sed -i '/simple_allow_groups =/s/$/,... (4 Replies)
Discussion started by: yash_message
4 Replies

2. Shell Programming and Scripting

Sed: -e expression #1, char 16: unterminated address regex

I am trying to grep for a particular text (Do action on cell BL330) in a text file(sample.gz) which is searched in the content filtered by date+timestamp (2016-09-14 01:09:56,796 to 2016-09-15 04:10:29,719) on a remote machine and finally write the output into a output file on a local machine. ... (23 Replies)
Discussion started by: rbadveti
23 Replies

3. Shell Programming and Scripting

awk throws makes too many open files

Hi, I have a below awk script. BEGIN { FS=","; } { system("curl -v -H \"Authorization: SSWS test" -H \"Accept: application/json\" -H \"Content-Type: application/json\" -X POST \"https://tes.test.com/api/v1/users?activate=false\" -d \'{ \"profile\": { \"firstName\": \"" $1 "... (7 Replies)
Discussion started by: Krrishv
7 Replies

4. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies

5. Shell Programming and Scripting

Calling shell script within awk script throws error

I am getting the following error while passing parameter to a shell script called within awk script. Any idea what's causing this issue and how to ix it ? Thanks sh: -c: line 0: syntax error near unexpected token `newline' sh: -c: line 0: `./billdatecalc.sh ... (10 Replies)
Discussion started by: Sudhakar333
10 Replies

6. Shell Programming and Scripting

Sed: -e expression #1, char 2: extra characters after command

Greetings.. getting the error while execution of the script, correct where i am missing #!/bin/bash DATE=`date +%Y-%m-%d:::%H:%M` HOSTNAME=`hostname` TXT="/log/temp.txt" LOGPATH="/log1/commanlogs/" IP=`/sbin/ifconfig | grep -i inet| head -n1| awk '{print $2}'| awk -F : '{print $2}'`... (7 Replies)
Discussion started by: manju98458
7 Replies

7. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

8. Shell Programming and Scripting

sed: -e expression #1, char 21: unterminated `s' command

I have read many threads, but I still didn't find the right answer. May be i didn't find the right thread, though are so many threads for the same question. Basically the situation is - find date in a file and replace it with another date. (its not homework, its part of lot of a big processing,... (10 Replies)
Discussion started by: avinthm
10 Replies

9. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

10. Shell Programming and Scripting

awk "Invalid char ' in expession" error

I have an HP PPM (ITG) application that is running an awk command in cygwin bash shell as part of ITG process moving SAP transports on a Windows 2003 server. The awk command checks the first two characters of a file containing return code that was retrieved from the SAP server. It is throwing the... (3 Replies)
Discussion started by: accsam1
3 Replies
Login or Register to Ask a Question

Featured Tech Videos