免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: mjus
打印 上一主题 下一主题

Regular expression matches ! [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-12-09 12:22 |只看该作者
原帖由 ly5066113 于 2008-12-9 11:16 发表
1、你想干什么?大家都在猜,现在还不知道猜对没猜对
2、想知道错误产生的原因?请读新手导航

你就是第一个猜的人。带领一批猜的人

论坛徽章:
0
12 [报告]
发表于 2008-12-10 01:23 |只看该作者
>1、你想干什么?大家都在猜,现在还不知道猜对没猜对
>2、想知道错误产生的原因?请读新手导航

Sorry about that. I can not keyin any chinese characters in my machine,
but can paste/cut the existing chinese characters.

(黑哥)'s comments reminded me of sed-version dependency. In fact, I tested all commands
in MKS (MK tools in windows). So I re-tested them in Linux. they work in Linux, but not
working what I expect. Below let me talk more in detais:

supposed we have a valid C file [named test.c],

void
foo(int a, int b) {
//impl. skipped
}   


void bigfunction() {
int x1, x2, y1, y2, z1, z2;

// after many lines
bar(); foo (x1, x2);


// after many lines
foo(y1,
      y2) ;


// after many lines
foo(
     z1,
     z2
    );

baz();
}


Now, let us create a command that populates the output as follows:
---------------
foo (x1, x2);
foo(y1,
      y2) ;
foo(
     z1,
     z2
   );
---------------

Note that don't print out the function implementation of foo,
Only print out the function calls of foo !



Hope this helps.

Thank you all !

论坛徽章:
0
13 [报告]
发表于 2008-12-10 01:38 |只看该作者
By the way, (黑哥)'s output got closer what I expect.

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
14 [报告]
发表于 2008-12-10 14:52 |只看该作者

回复 #12 mjus 的帖子

in Linux
sed -n '/foo/{x;/^$/b;x;:a;/) *;/!{N;ba};s/.*foo/foo/p}' test.c

MK tools in windows
  1. $ cat test.c
  2. void
  3. foo(int a, int b) {
  4. //impl. skipped
  5. }


  6. void bigfunction() {
  7. int x1, x2, y1, y2, z1, z2;

  8. // after many lines
  9. bar(); foo (x1, x2);


  10. // after many lines
  11. foo(y1,
  12.       y2) ;


  13. // after many lines
  14. foo(
  15.      z1,
  16.      z2
  17.     );

  18. baz();
  19. }
  20. $ cat test.sed
  21. #! C:\PROGRA~1\MKSTOO~1\mksnt/sed.exe -f
  22. /foo/{
  23.         x
  24.         /^$/b
  25.         x
  26.         :a
  27.         /) *;/!{
  28.                 N
  29.                 ba
  30.         }
  31.         s/.*foo/foo/p
  32. }
  33. $ sed -nf test.sed test.c
  34. foo (x1, x2);
  35. foo(y1,
  36.       y2) ;
  37. foo(
  38.      z1,
  39.      z2
  40.     );
复制代码

论坛徽章:
0
15 [报告]
发表于 2008-12-11 03:09 |只看该作者
Binggo ! your solutions are perfect !

Master Tim, I am so curious about SED.

May I ask you one more question ?


look back at test.c above, if I would like to print out

the function implementation of foo only (Not the function calls of foo).


That is

> sed SomeMagicCode test.c


The output is below
------------------------
void
foo(int a, int b) {
//impl. skipped
}
------------------------

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
16 [报告]
发表于 2008-12-11 09:54 |只看该作者

回复 #15 mjus 的帖子

这个有点复杂。
1、foo的前面是不是一定有void?
2、//impl. skipped 这个部分是否有 {}

论坛徽章:
0
17 [报告]
发表于 2008-12-11 11:40 |只看该作者
> 这个有点复杂。
> 1、foo的前面是不是一定有void?
> 2、//impl. skipped 这个部分是否有 {}


let me say something on it

1. as a C function, so returning type of a function could be void, int, float, double, char, char*, char**, user-defined structure, etc ... 。

2.  impl. section of course could be included {}, it is valid in C  。

Thank you so much !!!

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
18 [报告]
发表于 2008-12-12 10:47 |只看该作者

回复 #17 mjus 的帖子

test.sed
  1. #! /bin/sed -f
  2. N
  3. /foo/{
  4.         :a
  5.         /{/{
  6.                 x
  7.                 s/^/./
  8.                 x
  9.         }
  10.         /}/{
  11.                 x
  12.                 s/.//
  13.                 /^$/{
  14.                         x
  15.                         p
  16.                         q
  17.                 }
  18.                 x
  19.         }
  20.         p
  21.         n
  22.         ba
  23. }
  24. D
复制代码


局限性:
1、returning type 只能在 foo 的同行或者上一行
2、函数体内,同一行不能出现多余一个的{或},{和}同时出现没问题

论坛徽章:
0
19 [报告]
发表于 2008-12-12 11:31 |只看该作者
Wow !!!

Regardless of 局限性, it is the elegant solution (my 5 star) ! Can somebody beat it ???

Thank you, Tim !

论坛徽章:
0
20 [报告]
发表于 2008-12-12 12:08 |只看该作者
x 命令是为了干啥
没看懂~~
说说啥思路?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP