Sponsored Content
Top Forums Shell Programming and Scripting How do I split file into pieces with PERL? Post 302432066 by pseudocoder on Wednesday 23rd of June 2010 07:32:53 PM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
SC_ALLY(1)						    BSD General Commands Manual 						SC_ALLY(1)

NAME
sc_ally -- scamper driver to run Ally on a list of candidate aliases. SYNOPSIS
sc_ally [-?D] [-i infile] [-o outfile] [-p port] [-U unix-socket] [-w waittime] [-q attempts] [-t logfile] DESCRIPTION
The sc_ally utility provides the ability to connect to a running scamper(1) instance and have a set of IPv4 address-pairs testsed for aliases using the Ally technique. For each address pair in the file, sc_ally establishes which probe methods (UDP, TCP-ack, ICMP-echo) solicit an incrementing IP-ID value, and then uses the Ally technique on pairs where a probe method is able to obtain an incrementing IP-ID for both addresses. The output is written to a warts file. The options are as follows: -? prints a list of command line options and a synopsis of each. -D causes sc_ally to detach and become a daemon. -i infile specifies the name of the input file which consists of a sequence of IPv4 address-pairs, one pair per line. -o outfile specifies the name of the output file to be written. The output file will use the warts format. -p port specifies the port on the local host where scamper(1) is accepting control socket connections. -U unix-socket specifies the name of a unix domain socket where scamper(1) is accepting control socket connections. -w waittime specifies the minimum length of time, in seconds, to wait between completing a measurement to a particular IP address and issuing the next. -q attempts specifies the number of times to try Ally when one of the addresses is unresponsive. -t logfile specifies the name of a file to log output from sc_ally generated at run time. EXAMPLE
Given a set of IPv4-address pairs in a file named infile.txt: 192.0.2.1 192.0.32.10 192.0.2.2 192.0.31.8 192.0.2.3 192.0.30.64 and a scamper(1) daemon listening on port 31337, then these address-pairs can be tested for aliases using sc_ally -i infile.txt -o outfile.warts -p 31337 SEE ALSO
N. Spring, R. Mahajan, and D. Wetherall, Measuring ISP topologies with Rocketfuel, Proc. ACM SIGCOMM 2002. scamper(1), sc_wartsdump(1), sc_warts2text(1) AUTHORS
sc_ally is written by Matthew Luckie <mjl@luckie.org.nz>. BSD
March 1, 2011 BSD
All times are GMT -4. The time now is 11:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy