首页 > 解决方案 > 如何在 awk 中生成错误消息列表?

问题描述

我有一个这样的文件:

#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

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

我该怎么做呢?

标签: regexawk

解决方案


$ 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" }

推荐阅读