You are not logged in.

#1 2017-03-21 12:09:32

Temetka
Member
From: Sol System, Western Spiral Arm
Registered: 2015-10-14
Posts: 549

[SOLVED] Help with sed / awk to edit output of acpi command

Actually, I am working on a conky. Since the conky command {battery_short} doesn't do what I want - I'll Explain:

I'd like to display the first letter of (what I think) are the three options to display. Bear with me I'm tired I hope this makes sense.

So (in a terminal) acpi returns this: Battery 0: Unknown, 99%

What I am interested in is 3

so if you run: acpi | awk '{print $3}' <(acpi)

It should return one of these words: Charging, Discharging, Unknown,
NOTE: It adds a comma at the end of each word

So I'm looking for a way to trim it to just the first letter w/out the comma.

I'd like to insert it into my conky line thusly:

${goto 95}${color3}${font CaviarDrams:size=10}BAT Stats: ##INSERT HERE## ${battery_bar 5,50} ${execi 10 (acpi -b | awk '{print +$4}')}%

I'm pretty sure this can be done with 'sed' but I can't figure it out. Any help would be appreciated.


EDIT: Sleep Deprivation brings wonderful thoughts

In addition to the above request:

Would it be possible to replace entire words?

So if the output of acpi -b is "Unknown" can we change it to "Charged"?

A laptop battery basically has 4 states:

1. Not Present
2. Charging
3. Discharging
4. Charged

Plus an accompanying percentage for charge remaining (if discharging) or time remaining to charge (if charging).

The thought struck me because what if some other user out there is googling acpi + conky weirdness / formatting and is trying to do something zany. Perhaps it could be of some use to them.

Last edited by Temetka (2017-03-21 13:07:27)


The meaning of life is to just be alive. It is so plain and so obvious
    and so simple. And yet everybody rushes aroound in a great panic
             as if it were necessary to achieve something beyond themselves.
                                                                                                             - Alan Watts

Offline

#2 2017-03-21 12:20:36

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,746

Re: [SOLVED] Help with sed / awk to edit output of acpi command

stuff="Battery 0: Unknown, 99%"
echo "$stuff" | awk '{ print $3 }' | tr -d \,

should return

Unknown

p.s. Backslash is probably unnecessary. There is certainly a way to do all that with single awk and no tr.

edit: only awk:

echo "$stuff" | awk '{ gsub(/,/,""); print $3 }'

Last edited by brontosaurusrex (2017-03-21 12:38:02)

Offline

#3 2017-03-21 12:45:19

Temetka
Member
From: Sol System, Western Spiral Arm
Registered: 2015-10-14
Posts: 549

Re: [SOLVED] Help with sed / awk to edit output of acpi command

Thanks guys, I edited my OP with a 2nd request also.

That being said neither solution does what I want. I want to display ONLY the first character of the words "Charging, Discharging, or Unknown" depending on what acpi -b reports back. I can call the whole word myself with a simple command in the terminal:

acpi | awk '{print $3}' <(acpi)

It also adds a comma after the word. I presume so when a user runs just "acpi -b" from the terminal they get a formatted output. Here's mine from my laptop.

┌─(temetka@Samsara Tue, 21 Mar 17)────────────────────────────────(/home/temetka)─┐
└─(05:40 $)─> acpi -b
Battery 0: Charging, 99%, 00:09:47 until charged

So what I'm after is the C, D or U from "3" in terms of acpi | awk '{print $3}' <(acpi)

If I were to get really fancy I'd format the output thusly:

C = CH  (Charging)
D = DI  (Discharging)
U = FC  (Uknown, basically yeah, the battery is charged but stopped at 99% so for all intents and purposes it is fully charged)

I fully admit that I may be absolutely bonkers and this simply isn't possible. That's cool too.


The meaning of life is to just be alive. It is so plain and so obvious
    and so simple. And yet everybody rushes aroound in a great panic
             as if it were necessary to achieve something beyond themselves.
                                                                                                             - Alan Watts

Offline

#4 2017-03-21 12:55:20

Temetka
Member
From: Sol System, Western Spiral Arm
Registered: 2015-10-14
Posts: 549

Re: [SOLVED] Help with sed / awk to edit output of acpi command

I posted a scrot of the WIP in the monthly screen shot thread.

Look at the first sysinfo line for battery. I have 2 out of the 3 elements I want already (bar and percentage). Due to space constraints I'm attempting (with your help) to put the charge status in front of the bar.

Look here: https://forums.bunsenlabs.org/viewtopic … 468#p47468

EDIT: nobody is the man! Updated scrot.

Thanks again!

Last edited by Temetka (2017-03-21 13:06:57)


The meaning of life is to just be alive. It is so plain and so obvious
    and so simple. And yet everybody rushes aroound in a great panic
             as if it were necessary to achieve something beyond themselves.
                                                                                                             - Alan Watts

Offline

#5 2017-03-21 12:55:46

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,746

Re: [SOLVED] Help with sed / awk to edit output of acpi command

@nobody: nice smile

Offline

#6 2017-03-21 12:57:50

damo
....moderator....
Registered: 2015-08-20
Posts: 6,734

Re: [SOLVED] Help with sed / awk to edit output of acpi command

Look up the awk 'switch' statement - it allows the execution of statements based on a case match: The switch Statement

So if the awk variable is a match for "Unknown,", then output it as "FC" etc

EDIT: ninja'd by @nobody's neat one-liner!

Last edited by damo (2017-03-21 12:59:05)


Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#7 2017-03-21 12:58:15

Temetka
Member
From: Sol System, Western Spiral Arm
Registered: 2015-10-14
Posts: 549

Re: [SOLVED] Help with sed / awk to edit output of acpi command

nobody wrote:
acpi -b | sed -E 's/^.*(Charging|Discharging|Unknown).*$/\1/;s/Charging/CH/;s/Discharging/DI/;s/Unknown/FC/'

ZOMG!
C'est magnifique!

Thank you very much sir. You are a scholar and a gentleman!

smile


The meaning of life is to just be alive. It is so plain and so obvious
    and so simple. And yet everybody rushes aroound in a great panic
             as if it were necessary to achieve something beyond themselves.
                                                                                                             - Alan Watts

Offline

#8 2017-03-26 10:05:40

Temetka
Member
From: Sol System, Western Spiral Arm
Registered: 2015-10-14
Posts: 549

Re: [SOLVED] Help with sed / awk to edit output of acpi command

Well my new laptop has 2 battery bays, so everything is now either BAT0 or BAT1 and the above script no longer works correctly. That being said it should work fine on systems with a single battery.

Also after some googling I stumbled across this on the ArchLinux forums which seems to mostly handle what I need, so I'll throw it here.

ArchLinux Thread - https://bbs.archlinux.org/viewtopic.php?id=128343

Relevant info:

To hell with conky's variables! Try this:
Command to get battery percent:

acpi -b | awk "{print $1}" | sed 's/\([^:]*\): \([^,]*\), \([0-9]*\)%.*/\3/'

Command to get charger status ("discharging" / "charging" / "full"):

acpi -b | awk "{print $1}" | sed 's/\([^:]*\): \([^,]*\), \([0-9]*\)%.*/\2/'

Notice that the only difference is the index of the match in the sed expression: the second group is the status text, the third group is the percent value.


The meaning of life is to just be alive. It is so plain and so obvious
    and so simple. And yet everybody rushes aroound in a great panic
             as if it were necessary to achieve something beyond themselves.
                                                                                                             - Alan Watts

Offline

#9 2017-03-26 14:10:44

Temetka
Member
From: Sol System, Western Spiral Arm
Registered: 2015-10-14
Posts: 549

Re: [SOLVED] Help with sed / awk to edit output of acpi command

Or do 2 bars displaying the info for each battery.


The meaning of life is to just be alive. It is so plain and so obvious
    and so simple. And yet everybody rushes aroound in a great panic
             as if it were necessary to achieve something beyond themselves.
                                                                                                             - Alan Watts

Offline

Board footer

Powered by FluxBB