Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

How to use sed to perform a text filter

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

How to use sed to perform a text filter

#1 Post by PsySc0rpi0n »

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.

L_V
Posts: 1477
Joined: 2007-03-19 09:04
Been thanked: 11 times

Re: How to use sed to perform an

#2 Post by L_V »

For investigation:

Code: Select all

export AMOUNT=`awk '/amount/ { print $3 }' file.json`
echo  $AMOUNT
x.xxx,

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: How to use sed to perform an

#3 Post by PsySc0rpi0n »

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!

L_V
Posts: 1477
Joined: 2007-03-19 09:04
Been thanked: 11 times

Re: How to use sed to perform an

#4 Post by L_V »

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

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: How to use sed to perform an

#5 Post by PsySc0rpi0n »

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
phenest
Posts: 1702
Joined: 2010-03-09 09:38
Location: The Matrix

Re: How to use sed to perform an

#6 Post by phenest »

Code: Select all

"a previous command" | awk '/amount/ { print $3 }'
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: How to use sed to perform an

#7 Post by PsySc0rpi0n »

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!

Post Reply