• 0 Posts
  • 416 Comments
Joined 2 years ago
cake
Cake day: October 1st, 2023

help-circle













  • Sean Connery turned down both Gandalf in Lord of the Rings and Morpheus in The Matrix because he didn’t understand the scripts. He turned down Dumbledore in Harry Potter because it was too fantastical. Then he tried a reverse approach where he accepted the next role he didn’t understand, because clearly that was where the money was at. So he accepted Allan Quartermain in The League of Extraordinary Gentlemen, a movie which flopped hard. Then he ragequit the industry and retired.







  • Why do we replace the commas again with new lines?

    Consider this two-line output:

    $ echo 'a\nb'
    a
    b
    $
    

    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:

    $ echo 'a\nb' | tr '\n' ,
    a,b,$
    

    Substituting only the last comma ($ means end of line) allows us to get the output we expected:

    $ echo 'a\nb' | tr '\n' , | sed 's/,$/\n/'
    a,b
    $
    

    Or is there a way to combine them

    These two commands have equivalent output:

    tr '\n' ',' | tr ';' ',' 
    tr '\n;' ',,'
    

    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 tr to get the documentation for it.

    I tried What \w+\s+ Says About

    \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 About would 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 \w and \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.