开发者

Using a record separator of different length

开发者 https://www.devze.com 2023-01-15 18:01 出处:网络
I understand that the following script would print out line(s) separated by \'--\' (2 dashes), but how can I use it when there are many \'-\' (dashes)?

I understand that the following script would print out line(s) separated by '--' (2 dashes), but how can I use it when there are many '-' (dashes)?

开发者_C百科{
   local $/ = "--\n";
   while (<>) {
      chomp;
      print;
   }
}


You'll have to roll your own data stream parser. $/ isn't up to the task:

Remember: the value of $/ is a string, not a regex. awk has to be better for something. :-)


But a line that ends with three dashes and a newline is also a line that ends with two dashes and a newline. Wouldn't it be sufficient just to swap out the chomp command?

{
   local $/ = "--\n";
   while (<>) {
      chomp; s/\-+$//;    # chop off minimum record separator AND extra dashes
      print;
   }
}

or

       chomp && s/\-+$//

for the case where the last record in the data doesn't end with the record separator string.

0

精彩评论

暂无评论...
验证码 换一张
取 消