从C文件中提取标题名称的正则表达式 [英] Regular expression to extract header name from c file

查看:176
本文介绍了从C文件中提取标题名称的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从包含这样的头文件的c文件中提取头文件?

How to extract headers from a c file that contains them like this?

#include <tema4header9.h>
#include    <tema4header3.h>
#include   <stdio.h>
#include        <longnametest/newheader.h>
#include <net/header.h>
#include  "last-test-Zhy3/DrRuheader.h"
#include <last-test-8fF7/a5xyheader.h>

我尝试使用:

sed -n -e 's/#include[ \t]*[<"]\([^ \/<"]*\/[^ \/]*\)\.h[">]/\1\.h/p'

,但仅适用于子目录中的那些.另外,如果我输入:

but it only works for those in subdirectories. also if i type:

sed -n -e 's/#include[ \t]*[<"]\(([^ \/<"]*\/)+[^ \/]*\)\.h[">]/\1\.h/p'

sed -n -e 's/#include[ \t]*[<"]\(([^ \/<"]*\/)*[^ \/]*\)\.h[">]/\1\.h/p'

该命令不再起作用.输出文件应如下所示:

the command does not work anymore. The output file should look like this:

tema4header9.h
tema4header3.
stdio.h
longnametest/newheader.h
net/header.h
last-test-Zhy3/DrRuheader.h
last-test-8fF7/a5xyheader.h

推荐答案

grep解决方案:这是使用perl正则表达式,并在以#include开头的行上打印"<"'"'之间的任何内容.

grep solution: This is using perl regex and printing anything between "<" or '"' on the lines which start with #include.

grep -oP '^#include.*(<|")\K.*(?=>|")' headers
tema4header9.h
tema4header3.h
stdio.h
longnametest/newheader.h
net/header.h
last-test-Zhy3/DrRuheader.h
last-test-8fF7/a5xyheader.h

如果您对awk没问题:

awk '/#include/{gsub(/<|>|"/,"",$2);print $2}' headers
tema4header9.h
tema4header3.h
stdio.h
longnametest/newheader.h
net/header.h
last-test-Zhy3/DrRuheader.h
last-test-8fF7/a5xyheader.h

这篇关于从C文件中提取标题名称的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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