我需要帮助编写一个命令或 shell 脚本,以便按照以下详细信息生成 csv 文件 -
我的文件包含如下内容 -
+ Creates a new NotificationEventMapping
POST https://sbc.com:1234/openapi-fun-events/api/v1/notificationEventMapping [200 OK, 482B, 35ms]
+ Retrieves a NotificationEventMapping by Id_Mandatory
GET https://sbc.com:1234/openapi-fun-events/api/v1/notificationEventMapping/1043 [200 OK, 25.24kB, 50ms]
+ Updates the NotificationEventMapping identified by Id
PATCH https://sbc.com:1234/openapi-fun-events/api/v1/notificationEventMapping/1043 [200 OK, 491B, 37ms]
+ Deletes a NotificationEventMapping by Id
DELETE https://sbc.com:1234/openapi-fun-events/api/v1/notificationEventMapping/1043 [204 No Content, 146B, 10ms]
我需要过滤上述文件并以这种方式创建一个 csv 文件:
POST,Creates a new NotificationEventMapping,200,35
GET,Retrieves a NotificationEventMapping by Id_Mandatory,200,50
PATCH,Updates the NotificationEventMapping identified by Id,200,37
DELETE,Deletes a NotificationEventMapping by Id,204,10
GREP/AWK/SED 等,任何东西都可以使用。
谢谢 Pallav
答案1
和awk
:
$ awk 'BEGIN {
OFS = ","
FS = "[][ ]"
}
! /^\+/ && ! /^\s+$/ && NF && NF - 1 {
print $1, h, $4, $(NF - 1) + 0
}
/^\+/ {
sub(/^\+\s+/, "", $0)
h = $0
}' file
输出:
POST,Creates a new NotificationEventMapping,200,35
GET,Retrieves a NotificationEventMapping by Id_Mandatory,200,50
PATCH,Updates the NotificationEventMapping identified by Id,200,37
DELETE,Deletes a NotificationEventMapping by Id,204,10
... 并且file
是:
$ cat file
+ Creates a new NotificationEventMapping
POST https://sbc.com:1234/openapi-fun-events/api/v1/notificationEventMapping [200 OK, 482B, 35ms]
+ Retrieves a NotificationEventMapping by Id_Mandatory
GET https://sbc.com:1234/openapi-fun-events/api/v1/notificationEventMapping/1043 [200 OK, 25.24kB, 50ms]
+ Updates the NotificationEventMapping identified by Id
PATCH https://sbc.com:1234/openapi-fun-events/api/v1/notificationEventMapping/1043 [200 OK, 491B, 37ms]
+ Deletes a NotificationEventMapping by Id
DELETE https://sbc.com:1234/openapi-fun-events/api/v1/notificationEventMapping/1043 [204 No Content, 146B, 10ms]
怎么运行的:
一开始,BEGIN
输出字段分隔符OFS
设置为逗号,
,用于分隔输出中的字段,字段分隔符FS
设置为]
,以及从输入文件读取的每个输入行中拆分字段的[
空格... 之后,如果当前处理的行不以符号开头且不全是空格,并且字段数的计算结果大于 0,并且最后一个字段之前的字段数也大于 0,则打印第一个字段、变量中保存的值、第四个字段和最后一个字段之前的字段,通过将其放入数学计算中并在其中添加零来强制它仅打印数字整数,在这种情况下,除了从中剥离非数字字符外,这不会以任何方式改变原始数字... 另一方面,如果正在处理的行以加号开头,则从行中删除前导加号和其后的所有连续空格,然后将修改后的行存储在变量中,即...并循环。file
!
+
/^\+/
! /^\s+$/
NF
NF - 1
$1
h
$4
$(NF - 1)
+ 0
/^\+/
sub(/^\+\s+/, "", $0)
h
h = $0