I enjoy reading things like this. It’s posts like this that have helped me build my command line text processing skills over the years.
If you are early in your career, I suggest you work on these types of skills. It is surprising how often I have found myself on a random box that I needed to parse application logs “by hand”. This happens to me even in fancy, K8-rich environments.
This is very similar to "a11y"[0] and "i18n"[1]. The abbreviation of words using this technique has become surprisingly common in the software industry.
I'd say the vision impaired are going to understand what "ay-one-one-why" means about as fast as the rest of us. I'm not a fan of the cutesy letter-number jargon either, if you're typing about it in Slack, sure, okay, but it shouldn't escape confinement.
But it's equal-opportunity annoying I reckon: no one knows what the hell `a11y` is about when they first see/hear it, but not in a way that's more onerous for screen reader and braille users than for anyone else.
Sure, that's reasonable. Kind of circles back to "a11y" being technical language, which refers to a term of art, "accessibility", which is not identical to the word "accessibility" itself. This is at least part of why it gets used, although the main reason is really that a11y is easy to write and fast to read, while accessibility is neither.
Some code challenge sites offer all their challenges in bash - I highly recommend working through these if you want to get better at this type of stuff. Some problems are surprisingly simple, others torturously difficult.
Been a couple of years since I did any competitive problems for shell and cannot recall - however this hackerrank page is how I do interview prep for anything shell related -
> If you are early in your career, I suggest you work on these types of skills. It is surprising how often I have found myself on a random box that I needed to parse application logs “by hand”. This happens to me even in fancy, K8-rich environments.
It’s surprising how many times you have to ad hoc parse due to the tools being so poor. It’s endemic.
Regex can help you with fairly complicated source code edits too, like changing the order of parameters in some multi language project where there's no automated tool that can just do it.
$ sgpt -s "The command 'sp current' outputs
> Album Tea For The Tillerman (Remastered 2020)
> AlbumArtist Yusuf / Cat Stevens
> Artist Yusuf / Cat Stevens
> Title Wild World
> I want just 'Wild World by Yusuf/Cat Stevens'"
sp current | awk -F' +' '/Title/{title=$2} /Artist/{artist=$2} END{print title " by " artist}'
[E]xecute, [D]escribe, [A]bort: A
E
Wild World by Yusuf / Cat Stevens
Looking back at it, the awk command it uses is actually pretty clean.
It should use ^Title and ^Artist, otherwise something with "Artist" or "Title" will give wonky results.
More importantly, you can get the same results by just one or two dbus-send commands instead of using that "sp" script + something to clean it up.
The sp-metadata function uses tons of processes to clean the output[1], and sp-current launches a few more. If you're doing this in a loop for your WM status display this sort of stuff really adds up. Even on modern systems launching processes isn't free and relatively slow, and launching >20 of them every few seconds is going to use non-trivial amounts of CPU and will needlessly drain your battery.
I don't really know what that dbus command outputs, but I bet that with you might go a long way with "dbus-send ... | grep -o ..." or something.
So in general I'd say this is a classic case of "you're not even using the right solution, and no amount of GPT is going to help".
Shell scripting is the perfect use case for ChatGPT. Simple enough that AI can handle it, annoying enough that I don't really want to do it myself, and something I do rarely enough that I don't really know any of the commands deeply.
Out of curiosity I asked bog-standard web ChatGPT (4) if it could do the job in awk, took it five times to get it right. Whatever prompt shell-gpt is using, it works.
I'd prefer a general approach that used the first column as a key, and the rest as the value...into a dict/hash. Then if you need the Album title or something else later, it's easy to alter.
I'm sure awk could do that, but with Perl:
sp current | perl -nE '/(\S+)\s+(.*)/ and $d{$1}=$2;END{say "$d{Title} by $d{Artist}"}'
The omission of Perl from this post was pretty striking - not even mentioned in the final thoughts (instead thought to do the whole thing in awk??).
Perl has fallen from grace as a general programming language and even as a systems administration language, but it's still absolutely the best and most ubiquitous tool for text manipulation.
Oh, I wouldn't try to squeeze it down into a one liner. Maybe for a simple definition of the quoted text that doesn't need to account for edge cases like escaped quotes inside...
> MPRIS (Media Player Remote Interfacing Specification) is a standard D-Bus interface which aims to provide a common programmatic API for controlling media players.
> It provides a mechanism for discovery, querying and basic playback control of compliant media players, as well as a track list interface which is used to add context to the active media item.
There's a lot of better ways to do this. For starters, the sp script is bash. He could just edit the script. There's also a function for returning the metadata in machine-parseable syntax:
function sp-metadata {
# Prints the currently playing track in a parseable format.
dbus-send \
--print-reply `# We need the reply.` \
--dest=$SP_DEST \
$SP_PATH \
org.freedesktop.DBus.Properties.Get \
string:"$SP_MEMB" string:'Metadata' \
| grep -Ev "^method" `# Ignore the first line.` \
| grep -Eo '("(.*)")|(\b[0-9][a-zA-Z0-9.]*\b)' `# Filter interesting fiels.`\
| sed -E '2~2 a|' `# Mark odd fields.` \
| tr -d '\n' `# Remove all newlines.` \
| sed -E 's/\|/\n/g' `# Restore newlines.` \
| sed -E 's/(xesam:)|(mpris:)//' `# Remove ns prefixes.` \
| sed -E 's/^"//' `# Strip leading...` \
| sed -E 's/"$//' `# ...and trailing quotes.` \
| sed -E 's/"+/|/' `# Regard "" as seperator.` \
| sed -E 's/ +/ /g' `# Merge consecutive spaces.`
}
But as the other replier mentioned, the point was to show off an example of how text manipulation skills can solve many problems, not solve this specific problem in the best way possible.
I was scratching my head for a bit before I realized the final script in the article produces a slightly different output than the previous diff shows (Yusuf/Cat vs Yusuf / Cat). Anyway, here's a Nushell version. There's likely a way to use "detect columns" here but it doesn't seem to like the repeated value or something.
$ cat sp_out | lines | parse '{key} {value}' | str trim | transpose -rd | format pattern '{Title} by {Artist}'
Wild World by Yusuf / Cat Stevens
I use python more often than tools like awk, which I often forget the syntax of, so I made pyxargs to quickly run python code in the shell for small tasks like this
sp current | pyxr -0 -g "(Artist)\s+(.+)\n(Title)\s+(.+)" -p "{3} by {1}"
If you are early in your career, I suggest you work on these types of skills. It is surprising how often I have found myself on a random box that I needed to parse application logs “by hand”. This happens to me even in fancy, K8-rich environments.