注意:自从我写下这个答案以来,问题已经发生了很大的变化。
Replace [:space:]
with [[:space:]]
:
$ 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)
原因是[:space:]
将匹配任何字符:
, s
, p
, a
, c
, or e
。这不是你想要的。
你要[[:space:]]
它将匹配任何空格。
太阳/Solaris
众所周知,Sun/Solaris 的 awk 充满了错误。如果您在该平台上,请尝试nawk
or /usr/xpg4/bin/awk
or /usr/xpg6/bin/awk
.
使用 sed
可以使用非常相似的方法sed
。这使用基于您的正则表达式:
$ 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)
The -n
选项告诉 sed 不要打印,除非我们明确要求它打印。构造/.../p
告诉 sed 如果斜杠内的正则表达式匹配则打印该行。
Ed Morton 建议的所有对正则表达式的改进也适用于此处。
使用perl
以上也可以采用perl:
perl -ne 'print if /^[a-zA-Z_*]+[ \t]+[a-zA-Z*]+ *[(]/' dict3.c