awk不匹配所有的比赛我所有的条目 [英] Awk doesn't match all match all my entries

查看:160
本文介绍了awk不匹配所有的比赛我所有的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让脚本 - 本质上是一个awk命令 - 要提取的C $ C $的C函数的原型在.c文件自动生成一个头.H。
我是新使用awk,所以我没有得到所有细节。

I'm trying to make "a script" - essentially an awk command - to extract the prototypes of functions of C code in a .c file to generate automatically a header .h. I'm new with awk so I don't get all the details.

这是源.C的示例:

dict_t dictup(dict_t d, const char * key, const char * newval)
{

  int i = dictlook(d, key);

  if (i == DICT_NOT_FOUND) {

    fprintf(stderr, "key \"%s\" doesn't exist.\n", key);
    dictdump(d);
  }
  else {

    strncpy(d.entry[i].val, newval, DICTENT_VALLENGTH);
  }

  return d;
}


dict_t* dictrm(dict_t* d, const char * key) {

  int i = dictlook(d, key);

  if (i == DICT_NOT_FOUND) {

    fprintf(stderr, "key \"%s\" doesn't exist.\n", key);
    dictdump(d);
  }
  else {
    d->entry[i] = d->entry[--d.size];
  }
  if ( ((float)d->size)/d.maxsise < 0.25 ) {
    d->maxsize /= 2; 
    d->entry = realloc(d->entry, d->maxsize*sizeof(dictent_t*));
  }

  return d;
}

和我想要生成:

dict_t dictup(dict_t d, const char * key, const char *newval); 
dict_t* dictrm(dict_t* d, const char * key);

我与完整的regex命令看起来是这样的:

My command with the full regex looks like this :

 awk '/^[a-zA-Z*_]+[:space:]+[a-zA-Z*_]+[:space:]*\(.*?\)/{ print $0 }' dict3.c 

但我没有得到与它无关。
所以,我试着去挤它只是为了看看我是否可以拿出一些东西。
我试过这样:

But I don't get nothing with it. So I've tried to squeeze it just to see if I can come with something. I've tried this :

awk '/^[a-zA-Z*_]+[:space:]+[a-zA-Z*_]+/{ print $0 }' dict3.c 

和我得到的:

dictent_t* dictentcreate(const char * key, const char * val) 
dict_t* dictcreate() 
dict_t* dictadd(dict_t* d, const char * key, const char * val) 
dict_t dictup(dict_t d, const char * key, const char * newval) 
dict_t* dictrm(dict_t* d, const char * key) {

和它的许多奇迹的源泉!
第一个问题:
为什么一点儿也不首次正则表达式的工作?
以及为什么第二逮住了一些声明,但不是全部?
我向你保证,没有任何声明之前没有空格。我想这没赶上code一样,因为identation变量声明的另一部分。
第三个问题,为什么它逮住一切,我需要的只是前pression行?
最后一个bescause你是不错的,我怎么能增加;在每一个正则表达式的结束?

And it's source of lots of wonder ! First question : Why does'nt the first regex work ? And why the second has catched some of the declarations, but not all ?? I assure you that there is no space before any declaration. I guess it didn't catch other part of the code like variables declarations because of the identation. Third question, why has it catched all the line where I just need the expression ? Last one bescause you are nice, how can I add the ; at the end of each regex ?

感谢您的帮助,并随时纠正我的英语。

Thank you for your help and feel free to correct my english.

推荐答案

注意:因为我写这个答案的问题已有很大改变

替换 [:空间:] [:空间:]]

$ awk '/^[a-zA-Z*_]+[[:space:]]+[a-zA-Z*_]+[[:space:]]*[(].*?[)]/{ print $0 }' dict3.c
dictent_t* dictentcreate(const char * key, const char * val)  
dict_t* dictcreate() 
void dictdestroy(*dict_t d) 
void dictdump(dict_t *d) 
int dictlook(dict_t *d, const char * key) 
int dictget(char* s, dict_t *d, const char *key)
dict_t* dictadd(dict_t* d, const char * key, const char * val)
dict_t dictup(dict_t d, const char * key, const char *newval) 
dict_t* dictrm(dict_t* d, const char * key)

原因是, [:空间:] 将匹配任何字符,<$ C $ ç>取值, p A C 电子。这不是你想要的。

The reason is that [:space:] will match any of the characters :, s, p, a, c, or e. This is not what you want.

您想 [:空间:]]。将匹配任何空白

本机的Sun / Solaris上的awk是出了名的bug填充。如果你是在该平台上,尝试 NAWK 的/ usr / XPG4 /斌/的awk 在/ usr / xpg6 /斌/的awk

The native Sun/Solaris awk is notoriously bug-filled. If you are on that platform, try nawk or /usr/xpg4/bin/awk or /usr/xpg6/bin/awk.

有一个非常类似的方法可以用 SED 使用。它使用正则表达式根据你的:

A very similar approach can be used with sed. This uses a regex based on yours:

$ sed -n '/^[a-zA-Z_*]\+[ \t]\+[a-zA-Z*]\+ *[(]/p' dict3.c
dictent_t* dictentcreate(const char * key, const char * val)  
dict_t* dictcreate() 
void dictdestroy(*dict_t d) 
void dictdump(dict_t *d) 
int dictlook(dict_t *d, const char * key) 
int dictget(char* s, dict_t *d, const char *key)
dict_t* dictadd(dict_t* d, const char * key, const char * val)
dict_t dictup(dict_t d, const char * key, const char *newval) 
dict_t* dictrm(dict_t* d, const char * key)

-n 选项告诉sed不打印,除非我们明确的告诉它。该构建 /.../ P 告诉sed打印行,如果斜线内的正则表达式匹配。

The -n option tells sed not to print unless we explicitly ask it to. The construct /.../p tells sed to print the line if the regex inside the slashes is matched.

所有由埃德莫顿提出的正则表达式的改善也适用于此。

All the improvements to the regex suggested by Ed Morton apply here also.

以上也可以采用perl的:

The above can also be adopted to perl:

perl -ne  'print if /^[a-zA-Z_*]+[ \t]+[a-zA-Z*]+ *[(]/' dict3.c

这篇关于awk不匹配所有的比赛我所有的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆