0

I have a txt file and its content is as follows. I am trying leave only value like 12, [email protected] 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": "[email protected]",
"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,
    "[email protected]",
    "Rachel",
    "Howell",
    "https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg",
 }

What I expected is:

12
[email protected]
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
5
  • 1
    If this is a JSON value, why don't you use jq? Commented Jun 17, 2020 at 7:22
  • as mentioned by @oguzismail, you also check stackoverflow.com/questions/1955505/… Commented Jun 17, 2020 at 7: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 =) Commented Jun 17, 2020 at 7: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. Commented Jun 17, 2020 at 11:36
  • By the way I never used sed with the option -e on linux like for grep... is that really needed ? Commented Jun 17, 2020 at 14:51

2 Answers 2

2

With , it'd be:

jq -r '.[]' file
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

Why do we put the file name before cut instead of putting it at the end of command? Thaks! @ ATR
@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 }
Thank you for the feedback! @ATR

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.