Split files based on file content and pattern matching

前端 未结 6 2081
执笔经年
执笔经年 2020-12-15 22:45

I need your help with formate a txt file using bash/linux. The file looks like the following, it always has a line called Rate: Sth then it follows with the details in the v

6条回答
  •  不思量自难忘°
    2020-12-15 22:47

    I'd do this in perl:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    open (my $out, ">-") or die "oops";
    
    while(<>)
    {
        if (m/^Rate: (\w+)/o)
        {
            close $out and open ($out, ">$1") or die "oops";
            next;
        }
    
        print $out $_
    }
    

    Use it like

    perl ./test.pl input.txt
    

提交回复
热议问题