0

I have a txt file and its content is as follows. I am trying leave only value like 12, rachel.howell@reqres.in and delete brackets, quotation mark, comma, and the key. Could someone help me what I miss or go wrong with my command line? Thank you!

{
"id": 12,
"email": "rachel.howell@reqres.in",
"first_name": "Rachel",
"last_name": "Howell",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg",
}

Now, the result I can output is that

{
    12,
    "rachel.howell@reqres.in",
    "Rachel",
    "Howell",
    "https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg",
 }

What I expected is:

12
rachel.howell@reqres.in
Rachel
Howell
https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg

Here is my command:

sed -e 's/[{]//g' | cut -d':' -f '2' test.txt
oguz ismail
  • 1
  • 16
  • 47
  • 69
Oscar
  • 125
  • 1
  • 9
  • 1
    If this is a JSON value, why don't you use [tag:jq]? – oguz ismail Jun 17 '20 at 07:22
  • as mentioned by @oguzismail, you also check https://stackoverflow.com/questions/1955505/parsing-json-with-unix-tools – amit bhosale Jun 17 '20 at 07:26
  • 2
    `jq` is a good way to achieve what I expected, but I would like to practice using `sed`, `cut` to make the same result =) – Oscar Jun 17 '20 at 07:31
  • `sed` can manage json files only, if they use simple data structures and then always the same. Pitfalls are recursive data structures or different members/order/datatypes/line breaks. So it is always better to use context sensitive json tools like `jq`. – Wiimm Jun 17 '20 at 11:36
  • By the way I never used sed with the option -e on linux like for grep... is that really needed ? – ATR Jun 17 '20 at 14:51

2 Answers2

2

With , it'd be:

jq -r '.[]' file
oguz ismail
  • 1
  • 16
  • 47
  • 69
0

If you only wanna use sed cut and grep, I would recommand you that :

sed "s/}//g;s/{//g;s/\"//g;s/,//g;s/\ //g" test.txt | cut -d":" -f2 | egrep "..*"

But there's way more officient.

--

EDIT : description of the command :

sed "s/}//g;s/{//g;s/\"//g;s/,//g;s/\ //g"

-> removes {}",

cut -d":" -f2

-> only takes the 2nd part

egrep "..*"

-> only takes the lines which contain something

ATR
  • 107
  • 6
  • Why do we put the file name before `cut` instead of putting it at the end of command? Thaks! @ ATR – Oscar Jun 17 '20 at 08:32
  • @Oscar Sed must have an entry and this one is the file test.txt. Then the pipe "|" gives sed's output to cut and from cut to grep. If you specify the file only for cut, sed won't actually have anything to do with. That's why in your command output there was still the { and } – ATR Jun 17 '20 at 11:08
  • 1
    Thank you for the feedback! @ATR – Oscar Jun 17 '20 at 14:12