开发者

special characters causing problem in perl

I want to substitute all occurences of string in a file as follows :

printf("thread %d: enters barrier at %d and leaves at %d\n", MYTHREAD, start, end);

with

printf("thread %d: enters barrier at %lf and leaves at %dlf\n", MYTHREAD, (double开发者_开发百科)start, (double)end);

The command I have been trying is

perl -pi -e "s/printf(\"thread %d: enters barrier at %d and leaves at %d\\\n\", MYTHREAD, start, end);/printf(\"thread %d: enters barrier at %lf and leaves at %lf\\\n\", MYTHREAD,(double)start/CLOCKS_PER_SEC, (double)end/CLOCKS_PER_SEC)/g" bt_copy.c

But I get errors. Can anybody point out where I'm going wrong ?


You are using the slash / character as the delimiter for your s/// expression, but you also have the slash character in your replacement pattern

printf(\"thread %d: enters barrier at %lf and leaves at %lf\\\n\",
MYTHREAD,(double)start/CLOCKS_PER_SEC, (double)end/CLOCKS_PER_SEC)

You might try using a different delimiter, like

perl -pi -e 's! ...pattern ... ! ...replace ...!g' input_file

(Also if you are using a Unixy shell like bash, prefer single quotes to double quotes in specifying your one-line program. You will have less shell meta character interpolation related headaches that way).


From the C perspective, consider the merits of:

void pr_barrier_time(int thread, int start, int end)
{
     printf("thread %d enters barrier at %lf and leaves at %lf\n",
            thread, (double)start/CLOCKS_PER_SEC, (double)end/CLOCKS_PER_SEC);
}

And edit your code so the calls become:

pr_barrier_time(MYTHREAD, start, end);

You can even add an automatic 'fflush()' after the 'printf()' with the function call; much harder to do with the inline printf() statements.


This should do what you want using sed.

sed 's/printf("thread %d: enters barrier at %d and leaves at %d\\n", MYTHREAD, start, end);/printf("thread %d: enters barrier at %lf and leaves at %dlf\\n", MYTHREAD, (double)start, (double)end);/' bt_copy.c
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜