2

I'm trying to parse some \x00 null-delimited data. I thought that in a -pe/-ne loop, $_ would be equivalent to .* in the pattern matching side of a substitution regex... However as my example shows, they are not the same...

                                                  #      ↓↓‾‾ works, but is not a regex
printf 'aaa\n\0bbb\n\0' | perl -ne 'BEGIN{ $/="\0"; }; s/$_/\"$.$&\"/ and print;' |xxd
printf 'aaa\n\0bbb\n\0' | perl -ne 'BEGIN{ $/="\0"; }; s/.*/\"$.$&\"/ and print;' |xxd
                                                  #      ↑↑__ gives un-expected output

# output                                          #      ↓       ↓ 
0000000: 2231 6161 610a 0022 2232 6262 620a 0022  "1aaa..""2bbb.."
0000000: 2231 6161 6122 0a00 2232 6262 6222 0a00  "1aaa".."2bbb"..
                                                  #    ↑       ↑

What is needed to get an s/ / / type of substitution to recognize the full $_ ?

Peter.O
  • 6,696
  • 4
  • 30
  • 37

1 Answers1

5

. does not match \n unless the /s modifier is used.

printf 'aaa\n\0bbb\n\0' | perl -ne 'BEGIN{ $/="\0"; }; s/.*/\"$.$&\"/s and print;' |xxd

Alternately, a character class could be used instead of ..

printf 'aaa\n\0bbb\n\0' | perl -ne 'BEGIN{ $/="\0"; }; s/[\s\S]*/\"$.$&\"/ and print;' |xxd

Here, [\s\S] matches any space or non-space character (in other words, any character).

ephemient
  • 198,619
  • 38
  • 280
  • 391