1. 程式人生 > >分離出文字中c++字尾檔案cc和h名稱

分離出文字中c++字尾檔案cc和h名稱

有個需求,有一堆原始碼,需要給這些程式碼寫cmake檔案。一個檔名的複製貼上太慢。於是就想到採用awk的方式,對文字中的資料進行處理。將c++中的兩種字尾檔案分離出來。
-awk 匹配檔案字尾

BEGIN{
    cline_counter=0;
    hline_counter=0;
}
{
string=$1;
if(match(string,"[.]cc"))
{
    string_include_c[cline_counter]=string;
    cline_counter++;
}
if(match(string,"[.]h"))
{
    string_include_h[hline_counter]=string
; hline_counter++; } } END{ for(i=0;i<cline_counter;i++) { printf("%s\n",string_include_c[i]); } for(j=0;j<hline_counter;j++) { printf("%s\n",string_include_h[j]); } }

 例如,我要處理的文字內容
file.txt

base64.cc
base64.h
bind.h
bitbuffer.cc
bitbuffer.h
bitrateallocationstrategy.cc
bitrateallocationstrategy.h
buffer.h bufferqueue.cc bufferqueue.h bytebuffer.cc bytebuffer.h byteorder.h copyonwritebuffer.cc copyonwritebuffer.h event_tracer.cc event_tracer.h file.cc file.h flags.cc flags.h function_view.h ignore_wundef.h location.cc location.h numerics/histogram_percentile_counter.cc numerics/histogram_percentile_counter.h
numerics/mod_ops.h numerics/moving_max_counter.h onetimeevent.h pathutils.cc pathutils.h platform_file.cc platform_file.h race_checker.cc race_checker.h random.cc random.h rate_statistics.cc rate_statistics.h ratetracker.cc ratetracker.h string_to_number.cc string_to_number.h swap_queue.h template_util.h timestampaligner.cc timestampaligner.h trace_event.h zero_memory.cc zero_memory.h

 採用awk處理,awk -f cc.awk file >out.txt,最終輸出的out.txt的內容
out.txt

base64.cc
bitbuffer.cc
bitrateallocationstrategy.cc
bufferqueue.cc
bytebuffer.cc
copyonwritebuffer.cc
event_tracer.cc
file.cc
flags.cc
location.cc
numerics/histogram_percentile_counter.cc
pathutils.cc
platform_file.cc
race_checker.cc
random.cc
rate_statistics.cc
ratetracker.cc
string_to_number.cc
timestampaligner.cc
zero_memory.cc
base64.h
bind.h
bitbuffer.h
bitrateallocationstrategy.h
buffer.h
bufferqueue.h
bytebuffer.h
byteorder.h
copyonwritebuffer.h
event_tracer.h
file.h
flags.h
function_view.h
ignore_wundef.h
location.h
numerics/histogram_percentile_counter.h
numerics/mod_ops.h
numerics/moving_max_counter.h
onetimeevent.h
pathutils.h
platform_file.h
race_checker.h
random.h
rate_statistics.h
ratetracker.h
string_to_number.h
swap_queue.h
template_util.h
timestampaligner.h
trace_event.h
zero_memory.h