How do I split file into pieces with PERL?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I split file into pieces with PERL?
# 1  
Old 06-23-2010
How do I split file into pieces with PERL?

How do I split file into pieces with PERL?
IE
file.txt
head
1
2
3
4
end
head
5
6
7
8
9
end
n so on
# 2  
Old 06-23-2010
Hope you mean this type of splitting...
Code:
#!/usr/local/bin/perl

use strict;
use warnings;

my $infile='file.txt';
my $count=1;
my $outfile="$infile-section_$count.txt";
my @arr;

sub create_file {
open(OUT,">$outfile") or die "Error with outfile: $!\n";  
print OUT @arr;
close(OUT);
@arr=();
$count++;
$outfile="$infile-section_$count.txt";
}

open(IN,$infile) or die "Error with infile $infile: $!\n";
my @data=<IN>;
close(IN);

 foreach my $line (@data) {
  chomp($line);

  if ($line =~ /head/) {
  push @arr, "$line\n";
  next;
  }
  elsif ($line !~ /end/) {
  push @arr, "$line\n";
  next;
  }
  else {
  push @arr, "$line\n";
  create_file();
  }
 }

Code:
$ ./cutsections.pl
$ ls file.txt-*
file.txt-section_1.txt	file.txt-section_2.txt
$
$ cat file.txt-section_1.txt
head
1
2
3
4
end
$ cat file.txt-section_2.txt
head
5
6
7
8
9
end
$


Last edited by pseudocoder; 06-23-2010 at 08:38 PM..
This User Gave Thanks to pseudocoder For This Post:
# 3  
Old 06-23-2010
works
I'm new to perl I can accomplish this in bash or korn
Just trying to learn perl lol
# 4  
Old 12-12-2010
Hi, there,
I was looking for a script like the one proposed by pseudocoder.
I've given it a try and it works fine for me. But I want to repeat the process through a batch of files located in the same folder. How could I do it? Modifying the $infile value? But how? My perl knowledge is too shallow to do it myself.
Best
# 5  
Old 12-12-2010
Code:
awk 'FNR==1{i=0}/head/{p=++i}p{print > FILENAME"-part-"i}/end/{p=0}' file*


Last edited by Scrutinizer; 12-12-2010 at 09:18 AM..
# 6  
Old 12-12-2010
Thank you! I've tried it, but I got this message:

Quote:
macbook-pro-de-jmm:es02 jmmmac$ awk '/head/{p=1;i++}p{print > FILENAME"-part-"i}/end/{p=0}' file*
awk: syntax error at source line 1
context is
/head/{p=1;i++}p{print > >>> FILENAME"-part-" <<<
awk: illegal statement at source line 1
awk: illegal statement at source line 1
macbook-pro-de-jose-manuel-martinez-martinez:es02 jmmmac$
By the way, might I write a regular expresion instead of head? Say,
Code:
<intervention.+?>

.

Thanks
# 7  
Old 12-12-2010
Try:
Code:
awk 'FNR==1{i=1}/head/{p=1;if(f)close(f);f=FILENAME"-part-"i++}p{print>f}/end/{p=0}' file*

You can use extended regular expressions instead of head..

Last edited by Scrutinizer; 12-12-2010 at 09:37 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Break a file into smaller pieces with same search string

please help to break a file into smaller pieces with same search string and delete the matched lines. like i have a below file <expression> some text ........ .......... </expression> <expression> some text ........ .......... </expression> <expression> some text ........... (11 Replies)
Discussion started by: nadeemrafikhan
11 Replies

2. Shell Programming and Scripting

PERL: split 2 part from singel file.

Hi, I would like to split single fine into two array .. Example: file.txt --------------Installation -------------------- #GXTOOL=GxTools-20130501.tar.gz GCSS=GExpLinux-BE-3700.0.12.37.tar.gz TOP=TOPLinux-BE-3700.0.6.21.tar.gz GHDER=GHDERLinux-BE-3700.0.6.20.tar.gz... (2 Replies)
Discussion started by: Mani_apr08
2 Replies

3. Shell Programming and Scripting

Perl : to split the tags from xml file

I do have an xml sheet as below where I need the perl script to filter only the hyperlink tags. <cols><col min="1" max="1" width="30.5703125" customWidth="1"/><col min="2" max="2" width="7.140625" bestFit="1" customWidth="1"/> <col min="3" max="3" width="32.28515625" bestFit="1"... (3 Replies)
Discussion started by: scriptscript
3 Replies

4. Shell Programming and Scripting

perl script to split the text file after every 4th field

I had a text file(comma seperated values) which contains as below 196237,ram,25-May-06,ram.kiran@xyz.com,204183,Pavan,4-Jun-07,Pavan.Desai@xyz.com,237107,ram Chandra,15-Mar-10,ram.krishna@xyz.com ... (3 Replies)
Discussion started by: giridhar276
3 Replies

5. Shell Programming and Scripting

Grab 2 pieces of data within a file

I am a newbie and what I have is a captured file of content. I want to be able to grab 2 pieces of data, multiple times and print them to the screen. DataFile owner: locke user: fun data size: 60 location: Anaheim owner: david user: work data size: 80 location: Orange my script... (2 Replies)
Discussion started by: greglocke
2 Replies

6. UNIX for Dummies Questions & Answers

How to split a huge file into small pieces (per 2000 columns)?

Dear all, I have a big file:2879(rows)x400,170 (columns) like below. I 'd like to split the file into small pieces:2879(rows)x2000(columns) per file (the last small piece will be 2879x170. So far, I only know how to create one samll piece at one time. But actually I need to repeat this work... (6 Replies)
Discussion started by: forevertl
6 Replies

7. Shell Programming and Scripting

Split a 30GB XML file into 16 pieces

I have a 30 GB XMl file which looks like this: <page> <title>APRIL</title> .........(text contents that I need to extract and store in 1.dat including the <title> tag) </page> <page> <title>August</title> ....(text contents that I need to store in 2.dat including the <title> tag) </page>... (13 Replies)
Discussion started by: shoaibjameel123
13 Replies

8. Shell Programming and Scripting

Perl Split for text in file

Hi all I have written Perl script to swap the strings in the second a third column from a text file. My input file format is : the|empty|the|det lake|empty|lake|conj_and was|empty|was|auxpass drained|empty|drained|conj_and birds|empty|bird|s|nn The expected output file format is... (11 Replies)
Discussion started by: my_Perl
11 Replies

9. Shell Programming and Scripting

how to get split output of a file, using perl script

Hi, I have file: data.log.1 ### s1 main.build.3495 main.build.199 main.build.3408 ###s2 main.build.3495 main.build.3408 main.build.199 I want to read this file and store in two arrays in Perl. I have following command, which is working fine on command prompt. perl -n -e... (1 Reply)
Discussion started by: ashvini
1 Replies

10. Shell Programming and Scripting

Help fix my garbage - File Split Program in Perl

Hi, I have the following, it doesn't work and I know it's crap code. The objective is to split a file with a givin number of codes such as: 01,02,03,...,99 Then return all records with each seperate identifier in a new file. The files being split have lrecl=500, recfm=F, and I... (4 Replies)
Discussion started by: mkastin
4 Replies
Login or Register to Ask a Question