GNU AWK中BEGINEND使用举例

xiaoxiao2021-02-28  24

        以下是使用gnu awk将test.cpp文件拆分成两个文件a.cpp和b.cpp,其中b.cpp仅存放test.cpp中的数据,其它内容存放在a.cpp文件中。

        test.cpp内容如下:

#include <stdio.h> #include <iostream> #include <string> int main() { // face size: [20, 30, 200, 400](x, y, width, height), detection time in 50 ms. // eye size: [5, 5, 10, 10](x, y, width, height), detection time in 10 ms. const std::string blog_name = "https://blog.csdn.net/fengbingchun"; unsigned char t = 4; const unsigned char _xx[5] __attribute__((aligned(64))) = { 0x02, 0x32, 0x42, 0x32, 0x69 }; typedef struct { int x, y, width, height; float score; } rect; const unsigned char _yy[7] __attribute__((aligned(64))) = { 0x12, 0x23, 0x54, 0x89, 0x01, 0x09, 0x26 }; const std::string github_name = "https://github.com/fengbingchun"; const unsigned char _zz[3] __attribute__((aligned(64))) = { 0x23, 0x78, 0x15 }; fprintf(stdout, "ok\n"); return 0; }

        test.awk内容如下:

#! /bin/bash/awk BEGIN { # 在BEGIN中FILENAME不起作用 print "// ===== start process file: " ARGV[1] # author为外部传进来的变量名 print "// Author: " author state = 0 offset = 0 } { if (match($0, "blog_name .* \"(.*)\"", s)) { blog = s[1] } if (match($0, "github_name .* \"(.*)\"", s)) { github = s[1] } if (FNR == 1) { print "// file name: " file1 > file1 print "// Author: " author >> file1 print "// ===== start process file: " FILENAME >> file1 print "" >> file1 print "// file name: " file2 > file2 print "// Author: " author >> file2 print "// ===== start process file: " FILENAME >> file2 print "" >> file2 print "unsigned char data[] = { " >> file2 } if (state == 0 && $0 ~ "^ const unsigned char") { state = 1 pos0 = index($0, "_") pos1 = index($0, "[") pos2 = index($0, "]") name = substr($0, pos0, pos1 - pos0) offsetstr = substr($0, pos1 + 1, pos2 - pos1 - 1) offsetl = 0 + offsetstr if (offsetl % 64 != 0) { remain = 64 - offsetl % 64 } else { remain = 0 } print " const unsigned char* "name" = NULL;" >> file1 print " const unsigned int "name"_offset = " offset ";" >> file1 offset += (offsetl + remain); print "// start data: " name >> file2 } else if (state == 1) { if ($0 ~ "^ };") { print "// end data: " name >> file2; print "," >> file2 print "// "name" remain: " remain >> file2; for (i = 0; i < remain; ++i) { printf("0x00, ") >> file2; } print "" >> file2 state = 0; } else { print $0 >> file2 } } else { print $0 >> file1 } } END { printf "};\n\n" >> file2 print "// blog: "blog" " >> file1 print "// github: "github" " >> file2 print "// ===== end process file: " FILENAME print "// ===== end process file: " FILENAME >> file1 print "// ===== end process file: " FILENAME >> file2 }

        在mark.sh文件中添加一句:

awk -f test.awk -v author="fengbingchun" -v file1="a.cpp" -v file2="b.cpp" test.cpp

        执行mark.sh:$ ./mark.sh

        自动生成的a.cpp文件如下:

// file name: a.cpp // Author: fengbingchun // ===== start process file: test.cpp #include <stdio.h> #include <iostream> #include <string> int main() { // face size: [20, 30, 200, 400](x, y, width, height), detection time in 50 ms. // eye size: [5, 5, 10, 10](x, y, width, height), detection time in 10 ms. const std::string blog_name = "https://blog.csdn.net/fengbingchun"; unsigned char t = 4; const unsigned char* _xx = NULL; const unsigned int _xx_offset = 0; typedef struct { int x, y, width, height; float score; } rect; const unsigned char* _yy = NULL; const unsigned int _yy_offset = 64; const std::string github_name = "https://github.com/fengbingchun"; const unsigned char* _zz = NULL; const unsigned int _zz_offset = 128; fprintf(stdout, "ok\n"); return 0; } // blog: https://blog.csdn.net/fengbingchun // ===== end process file: test.cpp

        自动生成的b.cpp文件内容如下:

// file name: b.cpp // Author: fengbingchun // ===== start process file: test.cpp unsigned char data[] = { // start data: _xx 0x02, 0x32, 0x42, 0x32, 0x69 // end data: _xx , // _xx remain: 59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // start data: _yy 0x12, 0x23, 0x54, 0x89, 0x01, 0x09, 0x26 // end data: _yy , // _yy remain: 57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // start data: _zz 0x23, 0x78, 0x15 // end data: _zz , // _zz remain: 61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; // github: https://github.com/fengbingchun // ===== end process file: test.cpp         GitHub: https://github.com/fengbingchun/Linux_Code_Test  
转载请注明原文地址: https://www.6miu.com/read-2650156.html

最新回复(0)