Regular expression to extract header name from c file

后端 未结 3 1291
南笙
南笙 2021-01-28 02:29

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

#include 
#include    
#include   
         


        
相关标签:
3条回答
  • 2021-01-28 02:46

    Try:

    awk '{match($0,/[<"].*[>"]/);print substr($0,RSTART+1,RLENGTH-2)}' Input_file
    
    0 讨论(0)
  • 2021-01-28 02:55

    This should work:

    sed -nr 's/#include\s+[<"]([^>"]+)[>"].*/\1/p'
    
    0 讨论(0)
  • 2021-01-28 03:02

    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
    

    If you are ok with 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
    
    0 讨论(0)
提交回复
热议问题