LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-22-2024, 04:50 AM   #1
mprowe
LQ Newbie
 
Registered: Mar 2024
Posts: 2

Rep: Reputation: 0
How to ask SED to remove ALL but PATTERN?


Hi,
I wish to build a csv file from the information in my MQTT server.
This is my command:
Code:
~> mosquitto_sub -h 192.168.xxx.yyy -t topic/sub1/sub2/sub3 -u myuser -P mypass > file.txt
This is fine but the output contains more data than I need:
Code:
{"electricitymeter":{"timestamp":"2024-03-22T07:44:58Z","energy":{"export":{"cumulative":0.000,"units":"kWh"}... truncated for brevity!
Can anyone suggest a sed command that I can pipe through, that will remove all but two patterns:
pattern 1: "timestamp":"2024-03-22T07:44:58Z",
pattern 2: "unitrate":0.15190,
Note that "timestamp" & "unitrate" are unique and fixed but the values (between the colon ':' and comma ',' are variable.

Best regards, Martin
 
Old 03-28-2024, 02:23 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
this is a json file and you ought to use a json parser to gather the required information instead of sed/grep/awk.
You can use for example perl, python or jq.
 
1 members found this post helpful.
Old 03-28-2024, 02:45 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Quote:
Originally Posted by mprowe View Post
Can anyone suggest a sed command that I can pipe through, that will remove all but two patterns:
Multitudes likely.
We are not here to do your bidding - make an effort, and explain where you have issues. Someone may be able to help.

The above is the canonical answer, but in this case, if that is representative data, sed (with regex) can handle it reasonably straight-forwardly.
 
Old 03-28-2024, 02:57 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,310
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
I second the recommendation to use perl, python or jq. Furthermore, use a pipe rather than a redirection so that the processing script can work on the data as it arrives. Though Perl and Python can subscribe to MQTT directly so a pipe would only be needed for jq.
 
Old 03-28-2024, 03:30 AM   #5
lvm_
Member
 
Registered: Jul 2020
Posts: 926

Rep: Reputation: 337Reputation: 337Reputation: 337Reputation: 337
If your sed fixation is strong you may use \1 subexpression references to leave only what you want - see https://www.gnu.org/software/sed/man...pressions.html But yes, there are better tools for that.
 
Old 03-28-2024, 06:05 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
My idea with grep -o
Code:
grep -Eo -e '"timestamp":"[^"]*"' -e '"unitrate":([-+0-9.Ee]+|"[^"]*")'
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [BASH][SED] Find pattern and replace it's [pattern's] last character czezz Programming 10 10-22-2020 07:43 AM
sed delete lines with pattern to pattern (exluding the second) Jykke Linux - Software 10 07-23-2018 02:43 AM
vi search multiple pattern and remove only exact matching pattern amateurscripter Linux - Newbie 4 05-07-2018 01:19 PM
[SOLVED] How to remove certain pattern files except another certain pattern files from a list? Mike_Brown Linux - Newbie 4 04-23-2016 12:30 AM
[SOLVED] sed and how to remove all lines after matched pattern transmutated Programming 5 06-13-2012 07:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration