Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
regex = "\\.*0+$";
subst = "";
system("echo "id "| awk '{sub(\\.*0+$," subst"); print}'");

It is giving the following error:

awk: cmd. line:1: {sub(\.*0+$,); print}
awk: cmd. line:1:      ^ backslash not last character on line

I tried this one as well

system("echo "id "| awk '{sub("regex"," subst"); print}'");

and This is also no worky.

 str="echo " id " | awk '{sub(" regex  "," subst "); print;}'" ; str | getline check;

This is giving me same exception

awk: cmd. line:1: {sub(\.*0+$, ); print}
awk: cmd. line:1:      ^ backslash not last character on line

But, on the command line, this function is working fine.

echo 444000 | awk '{sub("\\.*0+$",""); print}' -> 444
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

It works if you put your regex inside / / (didn't know you could do away with those at all), and make sure the substitution ends up quoted:

system("echo "id "| awk '{sub(/\\.*0+$/,\""subst"\"); print}'");

Or:

system("echo "id "| awk '{sub(/"regex"/,\""subst"\"); print}'");

But I'm not sure why you are not simply doing:

sub(/\.*0+$/,"",id);
print id;

to avoid that whole system call.

share|improve this answer
    
yes, that's what I was looking at as a matter of fact, I didn't know sub would accept a third argument. Thanks a lot! –  user14039 Feb 28 '12 at 23:14
    
Well we both learned something on this one :) –  Mat Feb 28 '12 at 23:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.