LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2007, 02:08 PM   #1
quadmore
LQ Newbie
 
Registered: May 2004
Location: Ottawa, Ontario, Canada
Distribution: Redhat 9
Posts: 20

Rep: Reputation: 0
awk escape character for colon in string


Hello,

I am calling a line of awk from a bash script.

MYSUBSTRING="<factories xmi:type=\"resources.url:URL\" xmi:id=\"URL"
POSITION=`awk -v MYSUBSTRING=$MYSUBSTRING '{ print index ($0, MYSUBSTRING)}' <<< ${line}`
echo $POSITION

awk does not like the colon character in the string. How do I escape it?

Error encountered:
awk: cmd. line:1: xmi:type="resources.url:URL"
awk: cmd. line:1 ^ syntax error


Cheers,
q

Last edited by quadmore; 02-27-2007 at 02:20 PM.
 
Old 02-27-2007, 03:55 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
You gotta buncha stuff to look at here.

First, this is my cut-and-paste of what you said you did, with some echo statements to show what's going on:

Code:
#!/bin/sh

MYSUBSTRING="<factories xmi:type=\"resources.url:URL\" xmi:id=\"URL"
POSITION=`awk -v MYSUBSTRING=$MYSUBSTRING '{ print index ($0, MYSUBSTRING)}' <<< ${line}`

echo x001
echo $MYSUBSTRING
echo x002
echo $POSITION
echo x003
Did I copy it right?

Second, the output I got from running that was this:

Code:
x001
<factories xmi:type="resources.url:URL" xmi:id="URL
x002

x003
Note: no error message at all. Maybe I didn't copy what you did correctly. Please recheck.

Third, what's that $0 doing in there? In a bash command line, $0 is expanded to be the name of the running script if it's in a double quoted or back quoted string; it's left alone if it's in a single quoted string. I think that's what you want here, but you're not getting it. You're getting the name of the program, because this is really part of the back-quoted string; the single quotes within the back-quoted string don't count for squat when bash is interpreting the line; they just get sent through as they are, and bash doesn't even care if it finds an even number of them.

I discovered that by modifying this script somewhat (and saving it as e2, obviously):

Code:
#!/bin/sh

MYSUBSTRING="<factories xmi:type=\"resources.url:URL\" xmi:id=\"URL"
echo x000 "MYSUBSTRING=$MYSUBSTRING '{ print index ($0, MYSUBSTRING)}' <<< ${line}"

POSITION=`awk -v MYSUBSTRING=$MYSUBSTRING '{ print index ($0, MYSUBSTRING)}' <<< ${line}`

echo x001
echo $MYSUBSTRING
echo x002
echo $POSITION
echo x003
... I got this output:

Code:
x000 MYSUBSTRING=<factories xmi:type="resources.url:URL" xmi:id="URL '{ print index (./e2, MYSUBSTRING)}' <<< 
x001
<factories xmi:type="resources.url:URL" xmi:id="URL
x002

x003
See that ./e2 in there?

Anyway, either come back here with comments, or just break it down into simpler pieces so you can see what's going on.

Hope this helps.
 
Old 02-27-2007, 04:56 PM   #3
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by quadmore
Hello,

I am calling a line of awk from a bash script.

MYSUBSTRING="<factories xmi:type=\"resources.url:URL\" xmi:id=\"URL"
POSITION=`awk -v MYSUBSTRING=$MYSUBSTRING '{ print index ($0, MYSUBSTRING)}' <<< ${line}`
echo $POSITION

awk does not like the colon character in the string. How do I escape it?

Error encountered:
awk: cmd. line:1: xmi:type="resources.url:URL"
awk: cmd. line:1 ^ syntax error


Cheers,
q
Quote shell variables when you use them:

Code:
    POSITION=`awk -v MYSUBSTRING="$MYSUBSTRING" '{...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
.bashrc is there an escape character for alias? acummings Slackware 11 01-13-2007 10:56 AM
forward slash being treated as escape character in shell? debiant Linux - General 1 07-19-2006 04:57 PM
Escape character ? juanb Linux - Newbie 2 08-31-2004 10:03 AM
Handle escape characters in a string Helene Programming 7 05-01-2004 11:43 PM
Escape character not working in shell script philipz Programming 1 04-29-2004 09:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration