Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Using sed within a awk statement.
I am having trouble with this awk statement.

awk '{
system ("sed 's/"$1" /"$2" /g' tempfile0" )
}' ipdata.data

I keep getting this error.

awk: cmd. line:1: system ("sed s/
awk: cmd. line:1:         ^ unterminated string


For those who like details read below -------------------


Part of a Korn shell script I am writing needs to pull a IP address
from a reference file and use that IP address to replace a phone
number in another load file.  For example.

ipaddressfile

2031230987 10.1.2.3
2033211234 10.2.3.4
2033453214 10.3.4.5

loadfile

2031230987 889 000928 Stop 2031230987@abc.abc.com FIL 07012004 1
928 18 BSID
2033453214 576 003133 Stop 2033453214@abc.abc.com FIL 07012004 1
928 18 BSID
2031230987 137 012744 Start 2031230987@abc.abc.com FIL 07012004 1
155 8 BSID

I need to change the lead phone number to its associated IP.  This is
what the results should look like.

10.1.2.3 889 000928 Stop 2031230987@abc.abc.com FIL 07012004 1    928
18 BSID
10.3.4.5 576 003133 Stop 2033453214@abc.abc.com FIL 07012004 1    928
18 BSID
10.1.2.3 137 012744 Start 2031230987@abc.abc.com FIL 07012004 1    155
8 BSID

Report this thread to moderator Post Follow-up to this message
Old Post
Tibbz
09-10-04 08:55 PM


Re: Using sed within a awk statement.
Tibbz <tibby_n@hotmail.com> wrote:
> I am having trouble with this awk statement.
>
> awk '{
> system ("sed 's/"$1" /"$2" /g' tempfile0" )
> }' ipdata.data
>
> I keep getting this error.
>
> awk: cmd. line:1: system ("sed s/
> awk: cmd. line:1:         ^ unterminated string

Error means what it says.   You have
'{
system ("sed '
So, change all your internal single quotes to
'''

>
>
> For those who like details read below -------------------
>
>
> Part of a Korn shell script I am writing needs to pull a IP address
> from a reference file and use that IP address to replace a phone
> number in another load file.  For example.
>
> ipaddressfile
>
> 2031230987 10.1.2.3
> 2033211234 10.2.3.4
> 2033453214 10.3.4.5
>
> loadfile
>
> 2031230987 889 000928 Stop 2031230987@abc.abc.com FIL 07012004 1
> 928 18 BSID
> 2033453214 576 003133 Stop 2033453214@abc.abc.com FIL 07012004 1
> 928 18 BSID
> 2031230987 137 012744 Start 2031230987@abc.abc.com FIL 07012004 1
> 155 8 BSID
>
> I need to change the lead phone number to its associated IP.  This is
> what the results should look like.
>
> 10.1.2.3 889 000928 Stop 2031230987@abc.abc.com FIL 07012004 1    928
> 18 BSID
> 10.3.4.5 576 003133 Stop 2033453214@abc.abc.com FIL 07012004 1    928
> 18 BSID
> 10.1.2.3 137 012744 Start 2031230987@abc.abc.com FIL 07012004 1    155
> 8 BSID

Change your 'ipaddressfile' to 'ipaddressfile.sed',
s/^2031230987/10.1.2.3/
s/^2033211234/10.2.3.4/
s/^2033453214/10.3.4.5/
and run 'sed' directly,
sed -f ipaddressfile.sed loadfile

Ref:
man sed
--
William Park <opengeometry@yahoo.ca>
Open Geometry Consulting, Toronto, Canada

Report this thread to moderator Post Follow-up to this message
Old Post
William Park
09-10-04 08:55 PM


Re: Using sed within a awk statement.
In article <55b5d7ea.0409100726.68af202d@posting.google.com>,
Tibbz <tibby_n@hotmail.com> wrote:
>I am having trouble with this awk statement.
>
>awk '{
>system ("sed 's/"$1" /"$2" /g' tempfile0" )
>}' ipdata.data
>
>I keep getting this error.
>
>awk: cmd. line:1: system ("sed s/
>awk: cmd. line:1:         ^ unterminated string

You should never use sed (or any of the other "lesser tools") when using AWK
.
There are basically 2 exceptions to this rule:
1) There is a small set of very obscure things that sed can do that
awk cannot.  I am not aware of what any of these are, but from time
to time, sedders will mention this, so I will take their word for it.
2) Performance.  If the input file is very large and/or the machine
very slow, sometimes you can get a performance boost by using
grep or sed or something like that (that is more lightweight than
AWK) to do some of the processing.  This is rare.


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
09-10-04 08:55 PM


Re: Using sed within a awk statement.
Kenny McCormack wrote:

> You should never use sed (or any of the other "lesser tools") when using A
WK.
> There are basically 2 exceptions to this rule:
> 	1) There is a small set of very obscure things that sed can do that
> 	   awk cannot.  I am not aware of what any of these are, but from time
> 	   to time, sedders will mention this, so I will take their word for it.

Anything with backrefrences, althought gawk suports these by the
non-POSIX extension gensub (do other AWKs?), but only for substitution,
not searching.

> 	2) Performance.  If the input file is very large and/or the machine
> 	   very slow, sometimes you can get a performance boost by using
> 	   grep or sed or something like that (that is more lightweight than
> 	   AWK) to do some of the processing.  This is rare.

It's also probably in most cases premature optimization, which is as we
all know, is evil (the very root of it, no less).


-Ed

--
(You can't go wrong with psycho-rats.)       (er258)(@)(eng.cam)(.ac.uk)

/d{def}def/f{/Times findfont s scalefont setfont}d/s{10}d/r{roll}d f 5/m
{moveto}d -1 r 230 350 m 0 1 179{1 index show 88 rotate 4 mul 0 rmoveto}
for /s 15 d f pop 240 420 m 0 1 3 { 4 2 1 r sub -1 r show } for showpage


Report this thread to moderator Post Follow-up to this message
Old Post
E. Rosten
09-10-04 08:55 PM


Re: Using sed within a awk statement.
In article <4141D5DA.5060007@my.sig>, E. Rosten <look@my.sig> wrote:
>Kenny McCormack wrote:
> 
>
>Anything with backrefrences, althought gawk suports these by the
>non-POSIX extension gensub (do other AWKs?), but only for substitution,
>not searching.

Well, I only use gawk and tawk, both of which have back-referencing in
substitutions (albeit implemented differently - and, of course, I prefer
the tawk method).  I'm not sure what you by "for searching".
 
>
>It's also probably in most cases premature optimization, which is as we
>all know, is evil (the very root of it, no less).

Is it "the *love of* premature optimization" ?

BTW, this would make a good question for "Street Smarts" - ask people what
"premature optimization" is - and see what kind of responses you get...


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
09-10-04 08:55 PM


Re: Using sed within a awk statement.
In article <chsle5$5u8$1@yin.interaccess.com>,
Kenny McCormack <gazelle@interaccess.com> wrote:
...
>the tawk method).  I'm not sure what you by "for searching".

This should, of course, read "I'm not sure what you mean by ..."


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
09-10-04 08:55 PM


Re: Using sed within a awk statement.
On 10 Sep 2004 08:26:42 -0700 in comp.lang.awk, tibby_n@hotmail.com
(Tibbz) wrote:

>I am having trouble with this awk statement.
>
>awk '{
>system ("sed 's/"$1" /"$2" /g' tempfile0" )
>}' ipdata.data
>
>I keep getting this error.
>
>awk: cmd. line:1: system ("sed s/
>awk: cmd. line:1:         ^ unterminated string

You have a quoting problem; what awk is seeing is:

awk '{ system("sed 's/"$1" /"$2" /g' tempfile0" )}' ipdata.data
^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
arg1                   arg2  arg3               arg4

try this instead:

awk '{ system("sed '"'"'s/"$1" /"$2" /g'"'"' tempfile0")}' ipdata.data
^^^                 ^^^
embedded single quotes

unless you're running under a DOS/Windows shell, in which case just
put the awk code in a file.

ISTM you might be better using join to do the lookup, by having both
input files start with the phone number, sorted in phone number order,
and then use awk to filter the output:

sort -o ipdata.sort ipdata.data ; join -a 1 ipdata.sort tempfile0 |...

--
Thanks. Take care, Brian Inglis 	Calgary, Alberta, Canada

Brian.Inglis@CSi.com  	(Brian[dot]Inglis{at}SystematicSW[dot]a
b[dot]ca)
fake address		use address above to reply

Report this thread to moderator Post Follow-up to this message
Old Post
Brian Inglis
09-10-04 08:55 PM


Re: Using sed within a awk statement.
Kenny McCormack wrote:

> Well, I only use gawk and tawk, both of which have back-referencing in
> substitutions (albeit implemented differently - and, of course, I prefer
> the tawk method).  I'm not sure what you by "for searching".


In sed, you can use backrefs in a regex, vis:

/\([0-9][0-9]*\)-\1/p

to print all lines of the form 123-123

In gawk, it would seem that one can only use them in the substitution,
not in the regex itself.
 
>
>
> Is it "the *love of* premature optimization" ?

I don't know. Could be either. Premature optimization is bad enough. But
love of it (*cough* linux *cough*) is just plain awful.

-Ed



--
(You can't go wrong with psycho-rats.)       (er258)(@)(eng.cam)(.ac.uk)

/d{def}def/f{/Times findfont s scalefont setfont}d/s{10}d/r{roll}d f 5/m
{moveto}d -1 r 230 350 m 0 1 179{1 index show 88 rotate 4 mul 0 rmoveto}
for /s 15 d f pop 240 420 m 0 1 3 { 4 2 1 r sub -1 r show } for showpage


Report this thread to moderator Post Follow-up to this message
Old Post
E. Rosten
09-10-04 08:55 PM


Re: Using sed within a awk statement.

Tibbz wrote:
> I am having trouble with this awk statement.
>
> awk '{
> system ("sed 's/"$1" /"$2" /g' tempfile0" )
> }' ipdata.data
>
> I keep getting this error.
>
> awk: cmd. line:1: system ("sed s/
> awk: cmd. line:1:         ^ unterminated string

Rather than trying to hack a solution to that, the real answer is one of:

a) Don't call sed from awk, just use awk or
b) Don't call sed from awk, call sed from a shell loop

In either case, don't call sed from awk.

Either solution is pretty trivial, but since this is comp.lang.awk:

awk 'NR == FNR {p2i[$1]=$2; next}
{$1 = p2i[$1]; print}' ipaddressfile loadfile

It could be abbreviated to:

awk 'NR==FNR{p2i[$1]=$2;next}$1=p2i[$1]' ipaddressfile loadfile

if you prefer brevity and are sure there will always be an IP address
for every phone number.

If you want to guard against phone numbers without IP addresses, and
handle them explicitly, do something like this:

awk 'NR == FNR {p2i[$1]=$2; next}
$1 in p2i {$1 = p2i[$1]; print; next}
{print "No IP addr: " $0}' ipaddressfile loadfile

Regards,

Ed.



>
> For those who like details read below -------------------
>
>
> Part of a Korn shell script I am writing needs to pull a IP address
> from a reference file and use that IP address to replace a phone
> number in another load file.  For example.
>
> ipaddressfile
>
> 2031230987 10.1.2.3
> 2033211234 10.2.3.4
> 2033453214 10.3.4.5
>
> loadfile
>
> 2031230987 889 000928 Stop 2031230987@abc.abc.com FIL 07012004 1
> 928 18 BSID
> 2033453214 576 003133 Stop 2033453214@abc.abc.com FIL 07012004 1
> 928 18 BSID
> 2031230987 137 012744 Start 2031230987@abc.abc.com FIL 07012004 1
> 155 8 BSID
>
> I need to change the lead phone number to its associated IP.  This is
> what the results should look like.
>
> 10.1.2.3 889 000928 Stop 2031230987@abc.abc.com FIL 07012004 1    928
> 18 BSID
> 10.3.4.5 576 003133 Stop 2033453214@abc.abc.com FIL 07012004 1    928
> 18 BSID
> 10.1.2.3 137 012744 Start 2031230987@abc.abc.com FIL 07012004 1    155
> 8 BSID

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
09-10-04 08:55 PM


Re: Using sed within a awk statement.
In article <4141EAD0.2080003@my.sig>, E. Rosten <look@my.sig> wrote:
>Kenny McCormack wrote:
> 
>
>
>In sed, you can use backrefs in a regex, vis:
>
>/\([0-9][0-9]*\)-\1/p
>
>to print all lines of the form 123-123
>
>In gawk, it would seem that one can only use them in the substitution,
>not in the regex itself.

I see.  As it turns out, I've actually had to do this sort of thing in
tawk, and it has to be done in two steps - where basically you use match(),
and then the backreferencing built into that function (RSTART & RLENGTH) to
figure out what else needs to match.  I would imagine that there is nothing
you can do in sed that you can't do in AWK with a little more coding.  But
I'm sure some eager sedder will come along with a counter-example.
 
>
>I don't know. Could be either. Premature optimization is bad enough. But
>love of it (*cough* linux *cough*) is just plain awful.

I was making a joke based on the idea that it is not money that is the root
of all evil, but rather "the love of money".  But just out of curiosity,
what is it about Linux that arouses your ire?


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
09-10-04 08:55 PM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:05 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.