Apple - Finding MacBook power status from command line (AC plugged in, or not)?
The pmset command has much shorter output than system_profiler:
pmset -g batt
The first line on my MacBook says "Now drawing from ..." either "AC Power" or "Battery Power" when it runs.
pmset -g batt | head -n 1 | cut -d \' -f2
You can just use the delimiter to and escape the single quote to get the extracted text.