perl regex modifier /m /s clarified

  1. By default, q($) + q(^) won’t match newline. /m targets q($) and q(^)
  2. By default, the dot q(.) won’t match newline. /s targets the dot.
  3. The /m and /s would get newline matched in different contexts.

Official doc says:

  1. /m Treat the string being matched against as multiple lines. That is, change "^" and "$" from matching the start of the string’s first line and the end of its last line to matching embedded start and end of each line within the string.
  2. /sTreat the string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.
  3. Used together, as /ms, they let the "." match any character whatsoever, while still allowing "^" and "$" to match, respectively, just after and just before newlines within the string.

bash stdout/stderr redirecting #pitfall

some/command > some.log 2>&1

I always put 2>&1 at the end. Reversing it could fail as described in ..

bash: split long command to multiple lines

I managed to split a huge g++ command line  to about 300 lines… much more readable.

The trick:

  • terminate each line with a space, a backslash and … NO trailing space
  • my perl one-liner must user four backslashes to insert that single backslash, then another \n
  • total there are 5 backslashes in a row.

Here’s the command

perl -pe "s/ -/ \\\\\n-/g"

bashslash escape: bash tricky rules

This is about shell interpreting the backslash sequence inside single-quote or double-quote.

Once bash does its parsing, it can pass the result to a command like perl or grep.

----Most escape sequences don't care about single-quote vs double-quote
$ echo "msgType\t"

$ echo "msgType\b"

# \b is meaningful in perl regex 🙂

----double backslash -- single-quote is simpler than double-quote
$ echo 'msgType\\'

$ echo "msgType\\"

----single quote within single-quoted string is very tricky:
$ echo 'msgType\'\' 

# in the above, the last \' is a second token, a single-char string.

$ echo $'msgType\''  # dollar sign is crucial

$ echo 'msgType\'' # somehow doesn't work without $