18
Looking for Complex Text Manipulation CLI tools
(programming.dev)
From Wikipedia, the free encyclopedia
Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).
Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.
Community icon by Alpár-Etele Méder, licensed under CC BY 3.0
This is not difficult to achieve at all with tools like
sedorawk. But unless you provide a concrete example input file or files, all we can do is point to those tools.Something like this?
Turned into
Both "Franchis" and "Cartoons" where removed/ not included with the other words.
If you wanted a somewhat cruder approach using basically ubiquitous tools, you could do something like this:
Here I'm first using
grep '^ *-'to get all lines starting with any amount of whitespace and a leading dash, then piping that togrep -v ': *$'to remove anything with a colon at the end (including those with whitespace after the colon), then usingtr '\n' ','to replace all newlines with commas, and thensed s'/,$/\n/'to replace the trailing comma with a newline again (although sed is finicky across platforms wrt newlines, so you may want to just replace it with an empty string instead).The above is hardly an efficient approach, but it does the job.
I think this is The solutions that makes the most sense to me
But i don't understand what
seddoes hereWhy do we replace the commas again with new lines?
Also, I figure a better way to group related terms
Using semicolons ";"
I figure i can replace them with commas using
trcommandBut do i just pipe
Into
Or is there a way to combine them. I don't see an option to do more than operation in
trmanualLastly, i have been trying to use regex to match
To
I just need to match The "X" There, the program takes care of the rest
I tried
On this website to match
But using the debugger, it only recgnize "The" and then stops
Consider this two-line output:
We convert the newlines to commas. Now there is a comma at the end of the last line as well, and because of no newline, the next prompt is at the end of the output:
Substituting only the last comma (
$means end of line) allows us to get the output we expected:These two commands have equivalent output:
What tr does is take a list of characters in parameter 1 and converts them to the equivalent position character in parameter 2. There's a little more to it (it supports ranges, for example), but this will do the job. To learn more you can run
man trto get the documentation for it.\w+\s+matches "at least one word character and then at least one whitespace character, and that's not what you want. "The MCU" is one or more word characters, then a space, and then one or more word characters again, and that second part you're not matching at all. In this case, you're probably better off making a negative matching group where you make sure you don't match across separators.What [^,;]+ Says Aboutwould match anything that's not a comma or semicolon, for instance.The other problem with regex is that every implementation does things differently. For example, sed would interpret that plus as a literal
+, so for sed syntax you'd need to use\+instead. It also does not support\wand\s, and whether to use(or\(for a literal parenthesis also varies between implementations. I often switch to Perl if I need to do some more complex regex shenanigans.That because the program/ add-on i am using, only requires certain keywords to blacklist videos
so if it find
What "X" Says Aboutin a Video Title , it doesn't need the rest of the sentence to blacklist the video.Th developer links to Firefox's developers Regex Documentation.
This is a snip-it of the the add-on Guide. I cant like to it cuz for some reason its only inside the extension but here is the add-on's page
We're talking about different halves. The regex
\w+\s+matches "The " ("The" followed by a space), not "The MCU".If you're feeling a little old school (and some might say masochistic), you could so a similar crude parser with a perl oneliner. This would be more efficient compute wise, but it's a bit of an acquired taste readability wise:
Here
perl -nmakes perl look at each line individually,chompstrips off the trailing newline, we match for/^\s*-\s*(.*[^:\s])\s*$/(a string starting with a dash and ending with something not a colon) and append the content of the matching parenthesis to an implicitly declared array@a. Then we add anEND{}block which will be executed after all lines are parsed, where we print the array joined on,.If you can't install a dedicated tool like
yqbut don't mind creating a standalone script, python would be able to do this out of the box on pretty much any computer, calculator or toaster you can get your hands on in 2026:This takes the first argument on the command line, parses it as yaml, finds all leaf nodes recursively, and prints a comma-separated list of the results.
If you can stick to valid YAML like your example is, you can use a reasonably short
yqcommand to get a comma-separated string of all scalar values:..goes down the tree recursively,scalarsfilters out only scalar values,[]around those two makes them an array, and piping it all tojoin(",")makes it into a comma-separated string.This is technically yaml I think, a list (with one entry) of lists that contains mostly single items but also one other list. You should be able to parse this with a yaml parser like pythons built in one.
Note that yaml is picky abiut the syntax though, so it wouldn't be able to handle deviations.