分享
三行代码  ›  专栏  ›  技术社区  ›  JL2210

如何在awk中生成错误消息列表? - How do I generate a list of error messages in awk?

  •  1
  • JL2210  · 技术社区  · 2 月前

    我有一个这样的文件:

    #ifndef _ERRNO_H
    #define _ERRNO_H 1
    
    #include <features.h>
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    typedef int errno_t;
    
    #define errno (*__errno_location())
    
    errno_t errno;
    
    #ifdef _GNU_SOURCE
    extern char *program_invocation_name, *program_invocation_short_name;
    #endif
    
    #define EPERM   1       /* Operation not permitted */
    #define ENOENT  2       /* No such file or directory */
    #define ESRCH   3       /* No such process */
    #define EINTR   4       /* Interrupted system call */
    #define EIO     5       /* I/O error */
    #define ENXIO   6       /* No such device or address */
    #define E2BIG   7       /* Argument list too long */
    #define ENOEXEC 8       /* Exec format error */
    #define EBADF   9       /* Bad file number */
    #define ECHILD  10      /* No child processes */
    #define EAGAIN  11      /* Try again */
    #define ENOMEM  12      /* Out of memory */
    #define EACCES  13      /* Permission denied */
    #define EFAULT  14      /* Bad address */
    #define ENOTBLK 15      /* Block device required */
    #define EBUSY   16      /* Device or resource busy */
    #define EEXIST  17      /* File exists */
    #define EXDEV   18      /* Cross-device link */
    #define ENODEV  19      /* No such device */
    #define ENOTDIR 20      /* Not a directory */
    #define EISDIR  21      /* Is a directory */
    #define EINVAL  22      /* Invalid argument */
    #define ENFILE  23      /* File table overflow */
    #define EMFILE  24      /* Too many open files */
    #define ENOTTY  25      /* Not a typewriter */
    #define ETXTBSY 26      /* Text file busy */
    #define EFBIG   27      /* File too large */
    #define ENOSPC  28      /* No space left on device */
    #define ESPIPE  29      /* Illegal seek */
    #define EROFS   30      /* Read-only file system */
    #define EMLINK  31      /* Too many links */
    #define EPIPE   32      /* Broken pipe */
    #define EDOM    33      /* Math argument out of domain of func */
    #define ERANGE  34      /* Math result not representable */
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif
    

    我想从这个文件中获取错误名称、数字和字符串,包括括号和空格(注意 error_description_string 来自C样式注释的内部):

    { error_number, error_name, "error_description_string" }
    

    我已经试过了:

    awk '/#define E/ {print "{", $3",", $2",", $4, "}"}' ../../include/errno.h
    

    尽管其他一切都是正确的,但错误描述字符串始终是 /* .

    我该怎么做?

    1 回复  |  直到 2 月前
        1
  •  4
  •   Ed Morton    2 月前
    $ awk -v OFS=',' '
         /#define E/ {
            desc = $0
            gsub(/.*\/\*[[:space:]]*|[[:space:]]*\*\//,"\"",desc)
            print "{ " $3, $2, desc " }"
         }
    ' file
    { 1,EPERM,"Operation not permitted" }
    { 2,ENOENT,"No such file or directory" }
    { 3,ESRCH,"No such process" }
    { 4,EINTR,"Interrupted system call" }
    { 5,EIO,"I/O error" }
    { 6,ENXIO,"No such device or address" }
    { 7,E2BIG,"Argument list too long" }
    { 8,ENOEXEC,"Exec format error" }
    { 9,EBADF,"Bad file number" }
    { 10,ECHILD,"No child processes" }
    { 11,EAGAIN,"Try again" }
    { 12,ENOMEM,"Out of memory" }
    { 13,EACCES,"Permission denied" }
    { 14,EFAULT,"Bad address" }
    { 15,ENOTBLK,"Block device required" }
    { 16,EBUSY,"Device or resource busy" }
    { 17,EEXIST,"File exists" }
    { 18,EXDEV,"Cross-device link" }
    { 19,ENODEV,"No such device" }
    { 20,ENOTDIR,"Not a directory" }
    { 21,EISDIR,"Is a directory" }
    { 22,EINVAL,"Invalid argument" }
    { 23,ENFILE,"File table overflow" }
    { 24,EMFILE,"Too many open files" }
    { 25,ENOTTY,"Not a typewriter" }
    { 26,ETXTBSY,"Text file busy" }
    { 27,EFBIG,"File too large" }
    { 28,ENOSPC,"No space left on device" }
    { 29,ESPIPE,"Illegal seek" }
    { 30,EROFS,"Read-only file system" }
    { 31,EMLINK,"Too many links" }
    { 32,EPIPE,"Broken pipe" }
    { 33,EDOM,"Math argument out of domain of func" }
    { 34,ERANGE,"Math result not representable" }