How to use sed to perform a text filter

Need help with C, C++, perl, python, etc?

How to use sed to perform a text filter

Postby PsySc0rpi0n » 2019-09-13 22:23

Hello...

I need to extract a piece of information out of an output of a previously ran command in my terminal. And this output is in a JSON format, so this is an object wrapped in {} and [] and other chars.
Within those brackets there are several fields which have names and their respective values. Something like the example below:

Code: Select all
Result:
[                   (array of json object)
  {
    "txid" : "txid",
    "vout" : n,
    "address" : "address",
    "label" : "label",
    "scriptPubKey" : "key"
    "amount" : x.xxx,
    "confirmations" : n,
    "redeemScript" : "script"
    "witnessScript" : "script"
    "spendable" : xxx,
    "solvable" : xxx,
    "desc" : xxx,
    "safe" : xxx
  }
  ,...
]


My goal is to extract the value from the field "amount" and send it into my bash script to be stored in a variable a used later.
Can this be one with 'sed', 'awk' or any other way?
Last edited by PsySc0rpi0n on 2019-09-14 20:10, edited 1 time in total.
User avatar
PsySc0rpi0n
 
Posts: 115
Joined: 2012-10-24 13:54
Location: Portugal

Re: How to use sed to perform an

Postby L_V » 2019-09-14 06:23

For investigation:
Code: Select all
export AMOUNT=`awk '/amount/ { print $3 }' file.json`
echo  $AMOUNT
x.xxx,
L_V
 
Posts: 1125
Joined: 2007-03-19 09:04

Re: How to use sed to perform an

Postby PsySc0rpi0n » 2019-09-14 12:18

L_V wrote:For investigation:
Code: Select all
export AMOUNT=`awk '/amount/ { print $3 }' file.json`
echo  $AMOUNT
x.xxx,


The output I need to filter comes from a previous command. Not from a file.
I mean, I need to run a command that produces an output like the example I posted above in the stdout (terminal window), and from that output I need to filter out the value of that 'amount' field!
User avatar
PsySc0rpi0n
 
Posts: 115
Joined: 2012-10-24 13:54
Location: Portugal

Re: How to use sed to perform an

Postby L_V » 2019-09-14 12:53

PsySc0rpi0n wrote:The output I need to filter comes from a previous command. Not from a file.
Then, first
Code: Select all
 "a previous command" > file.json
L_V
 
Posts: 1125
Joined: 2007-03-19 09:04

Re: How to use sed to perform an

Postby PsySc0rpi0n » 2019-09-14 15:43

Ok, thanks...

Although this might be useful, I think I got another work around that might not need to be filtered!

thanks
Psy
User avatar
PsySc0rpi0n
 
Posts: 115
Joined: 2012-10-24 13:54
Location: Portugal

Re: How to use sed to perform an

Postby phenest » 2019-09-14 17:57

Code: Select all
"a previous command" | awk '/amount/ { print $3 }'
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D
User avatar
phenest
 
Posts: 1704
Joined: 2010-03-09 09:38
Location: The Matrix

Re: How to use sed to perform an

Postby PsySc0rpi0n » 2019-09-14 20:08

phenest wrote:
Code: Select all
"a previous command" | awk '/amount/ { print $3 }'


That don't work. It comes out empty! Anyway, don't bother. I have another problem to solve now. I'll create new post, I guess!
User avatar
PsySc0rpi0n
 
Posts: 115
Joined: 2012-10-24 13:54
Location: Portugal


Return to Programming

Who is online

Users browsing this forum: techsavvy and 1 guest

fashionable