MOHAMED MOHAMED - 8 days ago 5
Linux Question

how to extract specific message from a line with bash

my file contains lines like that

aaa bbb ccc {"eee": "ggg", "mmm": "sss"}
aaa1 bbb1 {"eee": "ggg"}
aaa2 bbb2 ccc2
aaa3 bbb3 ccc3 {"eee": "ggg", "mmm": "sss"}


I want to extract from each line the mesage between
{}
. If there is no message between
{}
then return empty message.

How to do that in bash?

Answer

Try awk-

$cat file
aaa bbb ccc {"eee": "ggg", "mmm": "sss"}
aaa1 bbb1 {"eee": "ggg"}
aaa2 bbb2 ccc2 
aaa3 bbb3 ccc3 {"eee": "ggg", "mmm": "sss"}

$awk -F[{}]  '{print $2}' file

Output-
"eee": "ggg", "mmm": "sss"
"eee": "ggg"

"eee": "ggg", "mmm": "sss"

This command sets the delimiters to be the curly brackets.

Comments