How to truncate STDIN line length?

前端 未结 9 1840
醉酒成梦
醉酒成梦 2021-01-05 07:15

I\'ve been parsing through some log files and I\'ve found that some of the lines are too long to display on one line so Terminal.app kindly wraps them onto the next line. Ho

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-05 07:42

    You can use a tied variable that clips its contents to a fixed length:

    #! /usr/bin/perl -w
    
    use strict;
    use warnings
    use String::FixedLen;
    
    tie my $str, 'String::FixedLen', 4;
    
    while (defined($str = <>)) {
        chomp;
        print "$str\n";
    }
    

提交回复
热议问题