- 论坛徽章:
- 6
|
本帖最后由 stanley_tam 于 2016-03-04 23:27 编辑
来一发不用正则表达式的 - #!perl
- use strict;
- use warnings;
- use autodie;
- # input file name
- my $file = 'a.txt';
- open my $fh, '<', $file;
- while ( defined (my $line = readline $fh) ) {
- # body...
- chomp $line;
- my $replacement = q{};
- my $date = substr $line, 0, 19, $replacement;
-
- my $is_in_bracket = 0;
- my $str_within_bracket = q{};
- my $line_needed = $date;
- for my $char ( unpack("U*", $line) ){
- $char = pack('U', $char);
- if ( $char eq '[' ) {
- $is_in_bracket = 1;
- $str_within_bracket = q{};
- }
- elsif ( $char eq ']' ){
- $is_in_bracket = 0;
- $line_needed .= "|$str_within_bracket";
- }
- elsif ( $is_in_bracket ) {
- $str_within_bracket .= $char;
- }
- else {
- # do nothing
- }
- }
- print "$line_needed$/";
- }
- close $fh;
- __END__
复制代码 |
评分
-
查看全部评分
|