Split files based on file content and pattern matching

前端 未结 6 2086
执笔经年
执笔经年 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:56

    You can use something like this in perl -

    Perl Script:

    #!/usr/bin/perl
    
    undef $/;
    $_ = <>;
    $n = 0;
    
    for $match (split(/(?=Rate)/)) {
          open(O, '>temp' . ++$n);
          print O $match;
          close(O);
    }
    

    Execution:

    [jaypal~/temp]$ ./spl.pl temp.file
    
    [jaypal~/temp]$ **cat temp.file**
    Line No. Main Text
    1    Rate: GBP
    2    12/01/1999,90.5911501,Validated
         .....
         .....
    210  18/01/1999,90.954996,Validated
    211  Rate: RMB
    212  24/04/2008,132.2542,Validated
         .....
    1000 25/04/2008,132.2279,Validated
    1001 28/04/2008,131.69915,Validated
    1002 Rate: USD
    1003 21/11/11,-0.004419534,Validated
    
    [jaypal~/temp]$ cat temp1
    Line No. Main Text
    1    
    
    [jaypal~/temp]$ cat temp2
    Rate: GBP
    2    12/01/1999,90.5911501,Validated
         .....
         .....
    210  18/01/1999,90.954996,Validated
    
    211  
    
    [jaypal~/temp]$ cat temp3
    Rate: RMB
    212  24/04/2008,132.2542,Validated
         .....
    1000 25/04/2008,132.2279,Validated
    1001 28/04/2008,131.69915,Validated
    
    1002 [jaypal~/temp]$ cat temp4
    Rate: USD
    1003 21/11/11,-0.004419534,Validated
    [jaypal~/temp]$ 
    

提交回复
热议问题