How to use grep multiple pattern and remove the first pattern [closed]
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I don't know if that's the proper title for my question. Please pardon me with that.
The code that I've tried is not getting the exact output that I want.
This is my code that I've tried:
curl -s --request GET
http://10.10.5.242/api/v1/incidents
-H "Content-Type: application/json;"
-H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj"
| json_pp | grep -e id -e component_id
And it will output this
"component_id" : "4",
"id" : 1,
"id" : 2,
"component_id" : "4",
"id" : 3,
"component_id" : "4",
"component_id" : "4",
"id" : 4
"component_id" : "3",
"id" : 5,
"component_id" : "2",
"id" : 6,
This is the content of the api the I'm using http://10.10.5.242/api/v1/incidents
"meta":
"pagination":
"total": 6,
"count": 6,
"per_page": 20,
"current_page": 1,
"total_pages": 1,
"links":
"next_page": null,
"previous_page": null
,
"data": [
"id": 1,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-23 14:56:16",
"updated_at": "2018-02-26 08:37:11",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 2,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-23 15:39:52",
"updated_at": "2018-02-26 08:37:11",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 3,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 08:15:43",
"updated_at": "2018-02-26 08:37:12",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 4,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 08:19:12",
"updated_at": "2018-02-26 08:37:12",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 5,
"component_id": "3",
"name": "Service Unavailable",
"status": "2",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 10:01:32",
"updated_at": "2018-02-26 10:01:32",
"deleted_at": null,
"human_status": "Identified"
,
"id": 6,
"component_id": "2",
"name": "Service Unavailable",
"status": "2",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 10:03:38",
"updated_at": "2018-02-26 10:03:38",
"deleted_at": null,
"human_status": "Identified"
]
The output that I want is to get all the id of "component_id": "4", and will output this
"id" : 1,
"id" : 2,
"id" : 3,
"id" : 4,
What I've wanted is grep the component_id and will get the all of the values of id of component id.
Cause my plan is to bring those values in my for loop.
linux bash grep json
closed as off-topic by Jeff Schaller, meuh, Kiwy, Anthony Geoghegan, Thomas Nyman Feb 26 at 12:16
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." â Jeff Schaller, meuh, Kiwy, Anthony Geoghegan, Thomas Nyman
add a comment |Â
up vote
0
down vote
favorite
I don't know if that's the proper title for my question. Please pardon me with that.
The code that I've tried is not getting the exact output that I want.
This is my code that I've tried:
curl -s --request GET
http://10.10.5.242/api/v1/incidents
-H "Content-Type: application/json;"
-H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj"
| json_pp | grep -e id -e component_id
And it will output this
"component_id" : "4",
"id" : 1,
"id" : 2,
"component_id" : "4",
"id" : 3,
"component_id" : "4",
"component_id" : "4",
"id" : 4
"component_id" : "3",
"id" : 5,
"component_id" : "2",
"id" : 6,
This is the content of the api the I'm using http://10.10.5.242/api/v1/incidents
"meta":
"pagination":
"total": 6,
"count": 6,
"per_page": 20,
"current_page": 1,
"total_pages": 1,
"links":
"next_page": null,
"previous_page": null
,
"data": [
"id": 1,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-23 14:56:16",
"updated_at": "2018-02-26 08:37:11",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 2,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-23 15:39:52",
"updated_at": "2018-02-26 08:37:11",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 3,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 08:15:43",
"updated_at": "2018-02-26 08:37:12",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 4,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 08:19:12",
"updated_at": "2018-02-26 08:37:12",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 5,
"component_id": "3",
"name": "Service Unavailable",
"status": "2",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 10:01:32",
"updated_at": "2018-02-26 10:01:32",
"deleted_at": null,
"human_status": "Identified"
,
"id": 6,
"component_id": "2",
"name": "Service Unavailable",
"status": "2",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 10:03:38",
"updated_at": "2018-02-26 10:03:38",
"deleted_at": null,
"human_status": "Identified"
]
The output that I want is to get all the id of "component_id": "4", and will output this
"id" : 1,
"id" : 2,
"id" : 3,
"id" : 4,
What I've wanted is grep the component_id and will get the all of the values of id of component id.
Cause my plan is to bring those values in my for loop.
linux bash grep json
closed as off-topic by Jeff Schaller, meuh, Kiwy, Anthony Geoghegan, Thomas Nyman Feb 26 at 12:16
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." â Jeff Schaller, meuh, Kiwy, Anthony Geoghegan, Thomas Nyman
2
ahh... please don't cross post stackoverflow.com/questions/48980643/â¦
â Sundeep
Feb 26 at 4:25
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I don't know if that's the proper title for my question. Please pardon me with that.
The code that I've tried is not getting the exact output that I want.
This is my code that I've tried:
curl -s --request GET
http://10.10.5.242/api/v1/incidents
-H "Content-Type: application/json;"
-H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj"
| json_pp | grep -e id -e component_id
And it will output this
"component_id" : "4",
"id" : 1,
"id" : 2,
"component_id" : "4",
"id" : 3,
"component_id" : "4",
"component_id" : "4",
"id" : 4
"component_id" : "3",
"id" : 5,
"component_id" : "2",
"id" : 6,
This is the content of the api the I'm using http://10.10.5.242/api/v1/incidents
"meta":
"pagination":
"total": 6,
"count": 6,
"per_page": 20,
"current_page": 1,
"total_pages": 1,
"links":
"next_page": null,
"previous_page": null
,
"data": [
"id": 1,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-23 14:56:16",
"updated_at": "2018-02-26 08:37:11",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 2,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-23 15:39:52",
"updated_at": "2018-02-26 08:37:11",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 3,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 08:15:43",
"updated_at": "2018-02-26 08:37:12",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 4,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 08:19:12",
"updated_at": "2018-02-26 08:37:12",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 5,
"component_id": "3",
"name": "Service Unavailable",
"status": "2",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 10:01:32",
"updated_at": "2018-02-26 10:01:32",
"deleted_at": null,
"human_status": "Identified"
,
"id": 6,
"component_id": "2",
"name": "Service Unavailable",
"status": "2",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 10:03:38",
"updated_at": "2018-02-26 10:03:38",
"deleted_at": null,
"human_status": "Identified"
]
The output that I want is to get all the id of "component_id": "4", and will output this
"id" : 1,
"id" : 2,
"id" : 3,
"id" : 4,
What I've wanted is grep the component_id and will get the all of the values of id of component id.
Cause my plan is to bring those values in my for loop.
linux bash grep json
I don't know if that's the proper title for my question. Please pardon me with that.
The code that I've tried is not getting the exact output that I want.
This is my code that I've tried:
curl -s --request GET
http://10.10.5.242/api/v1/incidents
-H "Content-Type: application/json;"
-H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj"
| json_pp | grep -e id -e component_id
And it will output this
"component_id" : "4",
"id" : 1,
"id" : 2,
"component_id" : "4",
"id" : 3,
"component_id" : "4",
"component_id" : "4",
"id" : 4
"component_id" : "3",
"id" : 5,
"component_id" : "2",
"id" : 6,
This is the content of the api the I'm using http://10.10.5.242/api/v1/incidents
"meta":
"pagination":
"total": 6,
"count": 6,
"per_page": 20,
"current_page": 1,
"total_pages": 1,
"links":
"next_page": null,
"previous_page": null
,
"data": [
"id": 1,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-23 14:56:16",
"updated_at": "2018-02-26 08:37:11",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 2,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-23 15:39:52",
"updated_at": "2018-02-26 08:37:11",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 3,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 08:15:43",
"updated_at": "2018-02-26 08:37:12",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 4,
"component_id": "4",
"name": "Service Unavailable",
"status": "4",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 08:19:12",
"updated_at": "2018-02-26 08:37:12",
"deleted_at": null,
"human_status": "Fixed"
,
"id": 5,
"component_id": "3",
"name": "Service Unavailable",
"status": "2",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 10:01:32",
"updated_at": "2018-02-26 10:01:32",
"deleted_at": null,
"human_status": "Identified"
,
"id": 6,
"component_id": "2",
"name": "Service Unavailable",
"status": "2",
"visible": 1,
"message": "Server is not responding",
"scheduled_at": "2018-02-26 10:05:03",
"created_at": "2018-02-26 10:03:38",
"updated_at": "2018-02-26 10:03:38",
"deleted_at": null,
"human_status": "Identified"
]
The output that I want is to get all the id of "component_id": "4", and will output this
"id" : 1,
"id" : 2,
"id" : 3,
"id" : 4,
What I've wanted is grep the component_id and will get the all of the values of id of component id.
Cause my plan is to bring those values in my for loop.
linux bash grep json
edited Feb 26 at 9:54
Kusalananda
103k13202318
103k13202318
asked Feb 26 at 3:25
Ian Jaylo Guinto
12
12
closed as off-topic by Jeff Schaller, meuh, Kiwy, Anthony Geoghegan, Thomas Nyman Feb 26 at 12:16
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." â Jeff Schaller, meuh, Kiwy, Anthony Geoghegan, Thomas Nyman
closed as off-topic by Jeff Schaller, meuh, Kiwy, Anthony Geoghegan, Thomas Nyman Feb 26 at 12:16
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." â Jeff Schaller, meuh, Kiwy, Anthony Geoghegan, Thomas Nyman
2
ahh... please don't cross post stackoverflow.com/questions/48980643/â¦
â Sundeep
Feb 26 at 4:25
add a comment |Â
2
ahh... please don't cross post stackoverflow.com/questions/48980643/â¦
â Sundeep
Feb 26 at 4:25
2
2
ahh... please don't cross post stackoverflow.com/questions/48980643/â¦
â Sundeep
Feb 26 at 4:25
ahh... please don't cross post stackoverflow.com/questions/48980643/â¦
â Sundeep
Feb 26 at 4:25
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
Using jq
:
curl -s --request GET
http://10.10.5.242/api/v1/incidents
-H "Content-Type: application/json;"
-H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" |
jq '.data | select(.component_id=="4").id'
Assuming that the data is delivered in the JSON form that you quote in the question, this will generate
1
2
3
4
These are the id
of the objects in the data
part of the JSON that has component_id
equal to 4.
To get the exact output that you requested, use
jq -r '.data | select(.component_id=="4") | ""id": (.id),"'
For the given document, this generates
"id": 1,
"id": 2,
"id": 3,
"id": 4,
add a comment |Â
up vote
0
down vote
You can use this:
grep -B1 ""component_id": "4"" | grep ""id":" | sed 's/^ *//g'
B1
: get the line before the match toosed 's/^ *//g'
: remove leading and trailing spaces
Esample:
echo sample | grep -B1 ""component_id": "4"" | grep ""id":" | sed 's/^ *//g'
The result will be:
"id": 1,
"id": 2,
"id": 3,
"id": 4,
Hi thanks for answering, But I've already figured how can I fixed it. and this is my code. ` curl -s --request GET 10.10.5.242/api/v1/incidents -H "Content-Type: application/json;" -H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" | jsonpp | grep -B1 '"component_id": "4",' | grep -v '"component_id": "4",' | grep -o '[0-9]+' `
â Ian Jaylo Guinto
Feb 26 at 4:12
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Using jq
:
curl -s --request GET
http://10.10.5.242/api/v1/incidents
-H "Content-Type: application/json;"
-H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" |
jq '.data | select(.component_id=="4").id'
Assuming that the data is delivered in the JSON form that you quote in the question, this will generate
1
2
3
4
These are the id
of the objects in the data
part of the JSON that has component_id
equal to 4.
To get the exact output that you requested, use
jq -r '.data | select(.component_id=="4") | ""id": (.id),"'
For the given document, this generates
"id": 1,
"id": 2,
"id": 3,
"id": 4,
add a comment |Â
up vote
2
down vote
Using jq
:
curl -s --request GET
http://10.10.5.242/api/v1/incidents
-H "Content-Type: application/json;"
-H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" |
jq '.data | select(.component_id=="4").id'
Assuming that the data is delivered in the JSON form that you quote in the question, this will generate
1
2
3
4
These are the id
of the objects in the data
part of the JSON that has component_id
equal to 4.
To get the exact output that you requested, use
jq -r '.data | select(.component_id=="4") | ""id": (.id),"'
For the given document, this generates
"id": 1,
"id": 2,
"id": 3,
"id": 4,
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Using jq
:
curl -s --request GET
http://10.10.5.242/api/v1/incidents
-H "Content-Type: application/json;"
-H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" |
jq '.data | select(.component_id=="4").id'
Assuming that the data is delivered in the JSON form that you quote in the question, this will generate
1
2
3
4
These are the id
of the objects in the data
part of the JSON that has component_id
equal to 4.
To get the exact output that you requested, use
jq -r '.data | select(.component_id=="4") | ""id": (.id),"'
For the given document, this generates
"id": 1,
"id": 2,
"id": 3,
"id": 4,
Using jq
:
curl -s --request GET
http://10.10.5.242/api/v1/incidents
-H "Content-Type: application/json;"
-H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" |
jq '.data | select(.component_id=="4").id'
Assuming that the data is delivered in the JSON form that you quote in the question, this will generate
1
2
3
4
These are the id
of the objects in the data
part of the JSON that has component_id
equal to 4.
To get the exact output that you requested, use
jq -r '.data | select(.component_id=="4") | ""id": (.id),"'
For the given document, this generates
"id": 1,
"id": 2,
"id": 3,
"id": 4,
answered Feb 26 at 9:54
Kusalananda
103k13202318
103k13202318
add a comment |Â
add a comment |Â
up vote
0
down vote
You can use this:
grep -B1 ""component_id": "4"" | grep ""id":" | sed 's/^ *//g'
B1
: get the line before the match toosed 's/^ *//g'
: remove leading and trailing spaces
Esample:
echo sample | grep -B1 ""component_id": "4"" | grep ""id":" | sed 's/^ *//g'
The result will be:
"id": 1,
"id": 2,
"id": 3,
"id": 4,
Hi thanks for answering, But I've already figured how can I fixed it. and this is my code. ` curl -s --request GET 10.10.5.242/api/v1/incidents -H "Content-Type: application/json;" -H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" | jsonpp | grep -B1 '"component_id": "4",' | grep -v '"component_id": "4",' | grep -o '[0-9]+' `
â Ian Jaylo Guinto
Feb 26 at 4:12
add a comment |Â
up vote
0
down vote
You can use this:
grep -B1 ""component_id": "4"" | grep ""id":" | sed 's/^ *//g'
B1
: get the line before the match toosed 's/^ *//g'
: remove leading and trailing spaces
Esample:
echo sample | grep -B1 ""component_id": "4"" | grep ""id":" | sed 's/^ *//g'
The result will be:
"id": 1,
"id": 2,
"id": 3,
"id": 4,
Hi thanks for answering, But I've already figured how can I fixed it. and this is my code. ` curl -s --request GET 10.10.5.242/api/v1/incidents -H "Content-Type: application/json;" -H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" | jsonpp | grep -B1 '"component_id": "4",' | grep -v '"component_id": "4",' | grep -o '[0-9]+' `
â Ian Jaylo Guinto
Feb 26 at 4:12
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can use this:
grep -B1 ""component_id": "4"" | grep ""id":" | sed 's/^ *//g'
B1
: get the line before the match toosed 's/^ *//g'
: remove leading and trailing spaces
Esample:
echo sample | grep -B1 ""component_id": "4"" | grep ""id":" | sed 's/^ *//g'
The result will be:
"id": 1,
"id": 2,
"id": 3,
"id": 4,
You can use this:
grep -B1 ""component_id": "4"" | grep ""id":" | sed 's/^ *//g'
B1
: get the line before the match toosed 's/^ *//g'
: remove leading and trailing spaces
Esample:
echo sample | grep -B1 ""component_id": "4"" | grep ""id":" | sed 's/^ *//g'
The result will be:
"id": 1,
"id": 2,
"id": 3,
"id": 4,
answered Feb 26 at 3:55
Wissam Roujoulah
2,833316
2,833316
Hi thanks for answering, But I've already figured how can I fixed it. and this is my code. ` curl -s --request GET 10.10.5.242/api/v1/incidents -H "Content-Type: application/json;" -H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" | jsonpp | grep -B1 '"component_id": "4",' | grep -v '"component_id": "4",' | grep -o '[0-9]+' `
â Ian Jaylo Guinto
Feb 26 at 4:12
add a comment |Â
Hi thanks for answering, But I've already figured how can I fixed it. and this is my code. ` curl -s --request GET 10.10.5.242/api/v1/incidents -H "Content-Type: application/json;" -H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" | jsonpp | grep -B1 '"component_id": "4",' | grep -v '"component_id": "4",' | grep -o '[0-9]+' `
â Ian Jaylo Guinto
Feb 26 at 4:12
Hi thanks for answering, But I've already figured how can I fixed it. and this is my code. ` curl -s --request GET 10.10.5.242/api/v1/incidents -H "Content-Type: application/json;" -H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" | jsonpp | grep -B1 '"component_id": "4",' | grep -v '"component_id": "4",' | grep -o '[0-9]+' `
â Ian Jaylo Guinto
Feb 26 at 4:12
Hi thanks for answering, But I've already figured how can I fixed it. and this is my code. ` curl -s --request GET 10.10.5.242/api/v1/incidents -H "Content-Type: application/json;" -H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" | jsonpp | grep -B1 '"component_id": "4",' | grep -v '"component_id": "4",' | grep -o '[0-9]+' `
â Ian Jaylo Guinto
Feb 26 at 4:12
add a comment |Â
2
ahh... please don't cross post stackoverflow.com/questions/48980643/â¦
â Sundeep
Feb 26 at 4:25