[英]Sed to add each match to own line
I have this 我有这个
awk -F=":" -v RS="," '$1~/"name"/ {print}' | sed -e 's/^.*"name"[ ]*:[ ]*"//' -e 's/".*//'`
user1 user2 user3 user4 user5
I am trying to add a way of creating each match on its own line instead of side by side 我正在尝试添加一种在自己的行而不是并排创建每个匹配项的方法
user1
user2
user3
user4
user5
For a bonus, is there a way of telling the awk and sed to skip the first match of "name"? 要获得红利,有没有办法告诉awk和sed跳过“名称”的第一场比赛?
This is the output from the api 这是api的输出
{
"name": "users",
"self": "http://",
"users": {
"size": 5,
"items": [
{
"self": "http://",
"name": "admin",
"emailAddress": "admin@example.com",
"avatarUrls": {
},
"displayName": "admin",
"active": true
},
{
"self": "http://",
"name": "user1",
"emailAddress": "user1@example.com",
"avatarUrls": {
},
"displayName": "user1",
"active": true
},
{
"self": "http://",
"name": "user2",
"emailAddress": "user2@example.com",
"avatarUrls": {
},
"displayName": "user2",
"active": true
},
{
"self": "http://",
"name": "user3",
"emailAddress": "user3@example.com",
"avatarUrls": {
},
"displayName": "user3",
"active": true
}
],
"max-results": 50,
"start-index": 0,
"end-index": 3
},
"expand": "users"
}
Using just awk
to interpret the sample json data: 仅使用awk
来解释样本json数据:
$ awk -F: -v RS="," '$1~/"name"/ && f {gsub("[ \",]", "", $2); print $2} $1~/"name"/{f=1}' json
admin
edwin
niwde
other
The first match of name
is skipped due to the flag f
. 由于标志f
, name
的第一个匹配项被跳过。
Explanation of awk
code: awk
代码说明:
$1~/"name"/ && f {gsub("[ \\",]", "", $2); print $2}
This consists of a condition and a group of statements. 这由一个条件和一组语句组成。 The condition requires that the first field match "name"
and that the flag f
is nonzero. 条件要求第一字段匹配"name"
和为标志f
非零。 If both those conditions are met, then spaces, quotes, and commas are removed from the same second field and the second field is printed. 如果同时满足这两个条件,则将从同一第二字段中删除空格,引号和逗号,并打印第二字段。
In awk
, the default value for variables is zero (or blank). 在awk
,变量的默认值为零(或空白)。 Consequently, nothing is printed until we assign f
a non-zero value. 因此,在给f
分配非零值之前,不会打印任何内容。
$1~/"name"/{f=1}
If the first field matches on "name"
, then the flag f
is set to 1
. 如果第一个字段与"name"
匹配,则标志f
设置为1
。 Thus, f
is not set to 1
until after the first name field is seen. 因此,直到看到名字字段后, f
才设置为1
。 Thus, the first name field will not be printed but all after it will be. 因此,将不会打印名字字段,但会打印所有名字字段。
你可以试试这个sed
sed -n '/"name": "/{s/ *"name": "\([^"]*\)",/\1/;p}' YourFile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.