daily pastebin goal
14%
SHARE
TWEET

Untitled

a guest Sep 12th, 2018 51 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. Perl: how to split a file?
  2. *****3123123*****RAW
  3. text1
  4. text2
  5. *****2312354***RAW
  6. text3
  7.    
  8. *****3123123*****RAW    
  9. text1
  10. text2
  11.    
  12. *****312312354***RAW
  13. text3
  14.    
  15. open FILE, "<file";
  16. @file= <FILE>;
  17. close FILE;
  18. @lines = split (/(RAWn)/, "@file");
  19. foreach $value (@lines) {
  20.   if ($value =~ /[a-z]|[A-Z]|[1-9]/)  {
  21.     print ("$valuen");
  22.   }
  23. }
  24.    
  25. *****3123123*****RAW
  26.  
  27.  text1
  28.  text2
  29.  
  30. *****312312354***RAW
  31.  
  32.  text3
  33.    
  34. *****3123123*****RAW
  35.  text1
  36.  text2
  37.  
  38.  *****12354***RAW
  39.  text3
  40.    
  41. use strict;
  42. use warnings;
  43.  
  44. @ARGV or die "Input file required as command-line parametern";
  45.  
  46. my $out;
  47.  
  48. while (<>) {
  49.   if ( /(d+)*+RAW$/ ) {
  50.     open $out, '>', "$1.out" or die $!;
  51.     select $out;
  52.   }
  53.   print $_ if $out;
  54. }
  55.    
  56. use strictures;
  57. use File::Slurp qw(read_file write_file);
  58. my $raw = read_file('raw.txt', binmode => ':raw');
  59. my $header = qr/^ (?= [*]+ [0-9]+ [*]+ RAWn)/msx;
  60. my @chunks = split $header, $raw;
  61. # (
  62. #     "*****3123123*****RAWntext1ntext2n",
  63. #     "*****2312354***RAWntext3"
  64. # )
  65. for my $i (1..@chunks) {
  66.     write_file("File$i.txt", {binmode => ':raw'}, $chunks[$i-1]);
  67. }
  68.    
  69. #!usr/bin/perl
  70. my $fi, $fi2;
  71. my $line;
  72. my $i;
  73. my @lines;
  74. my @filenameparts;
  75. my $filename = "file_1.txt";
  76.  
  77. open($fi, "< original.txt");
  78. @lines = <$fi>;
  79. open ($fi2, " > $filename");
  80.  
  81. foreach (@lines)
  82. {
  83. if (($i > 0) and $_ =~ /RAW/)
  84. {
  85.     @filenameparts = split("_", $filename);
  86.     foreach (@filenameparts)
  87.     {
  88.         print "Woooo".$_;
  89.     }
  90.     @filenameparts[1] = substr(@filenameparts[1], 0, @filenameparts[1].length() - 5);
  91.     @filenameparts[1] = ($filenameparts[1] + 1);
  92.     $filename = @filenameparts[0]."_".@filenameparts[1].".txt";
  93.     print $filename;
  94.     close($fi2);
  95.     open ($fi2, " > $filename");
  96.     $i = 0;
  97.     print $fi2 $_;
  98.  
  99. }
  100. else
  101. {
  102.     print $fi2 $_;
  103. }
  104. $i++;
  105.  
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top