News for hackers. Stuff that matters.
Re:Linux Text Manipulation by tyingq on Thursday March 28, 2024 @01:51PM (#39855064)
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}"}'
Re:Linux Text Manipulation by Zhyl on Thursday March 28, 2024 @01:59PM (#39855170)
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.

Re:Linux Text Manipulation by undershirt on Thursday March 28, 2024 @04:18PM (#39856795)
Can you write a Perl program to replace the content of anything found inside double-quotes with the result of piping it to the command `fmt`?
Re:Linux Text Manipulation by shawn_w on Thursday March 28, 2024 @04:37PM (#39857012)
Easily, though you don't need to drag external programs like fmt into it - perl comes with a standard module for word wrapping.

Text::Balanced (for extracting the quoted text) and Text::Wrap would be the core of such a program.

https://perldoc.perl.org/Text::Balanced

https://perldoc.perl.org/Text::Wrap

Re:Linux Text Manipulation by undershirt on Thursday March 28, 2024 @06:01PM (#39857889)
Could you write it for me as a one-liner? I’d like to learn more Perl and this would help.
Re:Linux Text Manipulation by shawn_w on Thursday March 28, 2024 @07:09PM (#39858543)
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...
Re:Linux Text Manipulation by undershirt on Thursday March 28, 2024 @09:36PM (#39859629)
Just a normal program then? No edge cases needed
Re:Linux Text Manipulation by evh on Thursday March 28, 2024 @07:20PM (#39858637)

    awk '{ k = $1; sub("^[^ ]* *", "", $0); d[k] = $0; } END { print d["Title"], "by", d["Artist"]; }'
Personally I'm a bit of a sed fan

    sed -nE '/^Artist/{N;s/^Artist +(.*)\nTitle +(.*)/\2 by \1/p;}'