A
A
anton02112022-01-16 16:13:55
macOS
anton0211, 2022-01-16 16:13:55

How to find a line in a text file using grep?

Good afternoon. I wondered how to use grep on MAC OS. The question is this: there is a text file with many entries of the form:

"id": "5644561-46484-546545" , "number": "879876-5644561-546545" ,"reestr":"46878-6214-8794"

How can I make it so that if I enter ids and then it would display a line like 5644561-46484-546545: 46878-6214-8794

Tried:
grep'\"id\":\"{}\"'|grep '\"reestr\":\"[0-9]*' > result.txt


Strongly do not scold, I did it by analogy with Linux - but it doesn’t work (

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Roman Mirilaczvili, 2022-01-16
@2ord

Try jq . Pretty handy for processing and extracting data.

V
Voland69, 2022-01-16
@Voland69

For handling json documents IMHO it's better to use jq .
It has advanced features for filtering and searching by json, it is perfectly installed on a poppy through homebrew.

A
AVKor, 2022-01-16
@AVKor

I wondered how to use grep on MAC OS.

First of all, install GNU-th.
Hastily":
tr -d '[:space:]"' | awk -F',' '{print $1 $3}' | sed -e 's/id://' -e 's/reestr:/: /'

X
xotkot, 2022-01-16
@xotkot

Yes, json search

if you have a json file like this:
file.json
[
{"id": "5644561-46484-546545" , "number": "879876-5644561-546545" ,"reestr":"46878-6214-8794"},
{"id": "6644561-46484-546545" , "number": "979876-5644561-546545" ,"reestr":"56878-6214-8794"},
{"id": "7644561-46484-546545" , "number": "079876-5644561-546545" ,"reestr":"66878-6214-8794"}
]

then everything is quite simply done using the jq console utility :
$ jq -r '.[] | "\(.id): \(.reestr)"' file.json
5644561-46484-546545: 46878-6214-8794
6644561-46484-546545: 56878-6214-8794
7644561-46484-546545: 66878-6214-8794

if you need a specific id:
$ jq -r '.[] | select(.id == "7644561-46484-546545") | "\(.id): \(.reestr)"' file.json
7644561-46484-546545: 66878-6214-8794

or through an external variable:
$ jq -r --arg ID "7644561-46484-546545" '.[] | select(.id == $ID) | "\(.id): \(.reestr)"' file.json
7644561-46484-546545: 66878-6214-8794

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question