C語言中使用類似awk的功能

來源:互聯網
上載者:User

標籤:nbsp   efault   struct   extend   res   c語言   highlight   express   tps   

awk實在是太強大了, 非常複雜的任務,幾行代碼就可以解決, awk經常需要用到, 但是在c語言裡面, 調用system不太優雅, 能不能直接在c語言中直接調用呢,簡單實現了一些功能, 但大多數情況,夠用了,參考

https://github.com/yiifburj/my_tools/blob/master/simple_use_of_awk_in_c.c

 

後面有一個樣本, 可以參考, 使用方法主要是 填寫

struct awk_st{#define PATTERN_NUM 3#define PATTERN_SIZE 16    int pattern_num;    char pattern[PATTERN_NUM][PATTERN_SIZE];    awk_begin_t fun_begin;    awk_end_t fun_end;    char action_default[0];    awk_action_t actions[PATTERN_NUM];    char data[0];};

 pattern可以不寫, 但至少寫一個action,除非不需要, 多個pattern的時候個數要和action保持一致並且一一對應,同時要設定 pattern_num, pattern使用POSIX Extended Regular Expression , fun_begin fun_end fun_action都需要自己實現, 可以為NULL, data[0]指向的部分會被傳入到fun_begin fun_end fun_action中.

同時提供了字串替代函數

/* not found also return 0 */
int awk_str_replace_inplace(char *src, const char *old, const char *new);
int awk_str_replace(const char *src, const char *old, const char *new, char buf[], int bufsize);
int awk_str_replace_regex(const char *src, const char *pattern, const char *new, char buf[], int bufsize);
int awk_str_replace_regex_inplace(char *src, const char *pattern, const char *new);
失敗返回負數

 

詳情參考代碼。

C語言中使用類似awk的功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.