D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] Help with a little code for conky

 

Hi Anthony,
Try:

    sed -n '/<current>/,+1s/ *<[^>]*>//gp' aurora2.xml

To remove the leading blank line, pipe it through `tail -1' as well.

To break it down:
    sed -n    # use sed but don't automatically print the pattern space (ie the input)

    /<current>/,+1  # operate on the range of lines from the line matching <current> to that line plus one

    s/ *<[^>]*>//    # replace every < .... > tag with the empty string

    gp      # apply the replacement globally to every line and then print the result


All that said, the standard (nay, infamous!) advice is not to parse XML or HTML with regexes. See https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454

Hth,
Ian


On Fri, 7 Dec 2018 at 09:41, Pentiddy <pentiddy@xxxxxxxxxxxxxx> wrote:
Hi All,

As a little diversion I'm looking at presenting information from Aurorawatch API on conky.
Got somewhere but my programming skills now find me lacking...

So in .conkyrc file I have the following
${execi 300 grep "state" ~/.cache/conky-vision/aurora2.xml | grep -o "name=\"[^\"]*\"" | grep -o "\"[^\"]*\"" | grep -o "[^\"]*" | awk 'NR==1' | tr '[a-z]' '[A-Z]'}\

form the file aurora2.xml whose contents show

<aurorawatch>
  <current>
    <state name="green" value="0" color="#33ff33">No significant activity</state>
  </current>
  <previous>
    <state name="yellow" value="50" color="#ffff00">Minor geomagnetic activity</state>
  </previous>
  <station>SAMNET/LAN2. Lancaster, UK.</station>
  <updated>2018-12-07 08:45:32</updated>
</aurorawatch>

This displays "GREEN" (or whatever the status is...
I would simply like to display the last information on that line "No significant activity"
Any help with this would be gratefully received.
Thanks

Anthony

Pentiddy Logo. Pentiddy Woods / Pentiddy Woods newsletter

Attached files are likely to be open-document format (*.odt or *.ods).
Why? So as not to depend on one company for accessing my own data in future.
See:  https://www.documentliberation.org/
MS-Word may say 'damaged file' to discourage this but should open the attachment(s) anyway.

Pentiddy Natural Burials
Heartwood Creations
Pentiddy Community Woodland

--
The Mailing List for the Devon & Cornwall LUG
https://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/listfaq
-- 
The Mailing List for the Devon & Cornwall LUG
https://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/listfaq