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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get split output of a file, using perl script
# 1  
Old 08-25-2009
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 '/^###/ and open FH, ">output_".$n++; print FH;' data.log.1

But I want to use this command in Perl file, so that I can store output ###s1 (part) and ###s2 (part) in two different arrays.

Any help will be appreciated.

Thanks
# 2  
Old 08-25-2009
Here's one way to do it in Perl:

Code:
$ 
$ cat data.log.1
###s1
main.build.3495
main.build.199
main.build.3408

###s2
main.build.3495
main.build.3408
main.build.199
$ 
$ ##
$ perl -ne 'chomp;
>           if (/^###s1/) {$s1=true} elsif(/^###s2/) {$s2=true}
>           if ($s2) {push @s2,$_} elsif($_ ne "" && $s1) {push @s1,$_}
>           END {print "Array \@s1:\n";foreach $i (@s1) {print $i,"\n"}
>                print "Array \@s2:\n";foreach $i (@s2) {print $i,"\n"}}' data.log.1
Array @s1:
###s1
main.build.3495
main.build.199
main.build.3408
Array @s2:
###s2
main.build.3495
main.build.3408
main.build.199
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modification of perl script to split a large file into chunks of 5000 chracters

I have a perl script which splits a large file into chunks.The script is given below use strict; use warnings; open (FH, "<monolingual.txt") or die "Could not open source file. $!"; my $i = 0; while (1) { my $chunk; print "process part $i\n"; open(OUT, ">part$i.log") or die "Could... (4 Replies)
Discussion started by: gimley
4 Replies

2. Shell Programming and Scripting

Oneliner ---split string to character by piping shell output to perl

Hello, I was trying to split a string to characters by perl oneliner. echo "The quick brown fox jumps over the lazy dog" | perl -e 'split // ' But did not work as with bash script pipe: echo "The quick brown fox jumps over the lazy dog" | fold -w1 | sort | uniq -ic 8 1 T 1... (6 Replies)
Discussion started by: yifangt
6 Replies

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

4. Shell Programming and Scripting

perl script to split the filename

In PERL script I have few files named theme1.htm,theme2.htm,theme3.htm and so on. now I need to write perl code to split the the filename and store only that particular digit. Example -------------- filename is theme1.htm output should be 1 another example ---------------... (5 Replies)
Discussion started by: giridhar276
5 Replies

5. Shell Programming and Scripting

Output after a perl script gives a file with size zero.

Hi, I have a unix shell script which generates a flat file after connecting to Teradata servers to fetch tables and views and also picks up modified unix scripts from the specified paths. Later on the script calls a perl script to assign a value based on the type of object in the flat file which... (2 Replies)
Discussion started by: yohasini
2 Replies

6. Shell Programming and Scripting

split input data file and put into same output file

Hi All, I have two input file and need to generate a CSV file. The existing report just "GREP" the records with the Header and Tailer records with the count of records. Now i need to split the data into 25 records each in the same CSV file. id_file (Input file ) 227050994 232510151... (4 Replies)
Discussion started by: rasmith
4 Replies

7. Shell Programming and Scripting

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 (7 Replies)
Discussion started by: 3junior
7 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

Perl script error to split huge data one by one.

Below is my perl script: #!/usr/bin/perl open(FILE,"$ARGV") or die "$!"; @DATA = <FILE>; close FILE; $join = join("",@DATA); @array = split( ">",$join); for($i=0;$i<=scalar(@array);$i++){ system ("/home/bin/./program_name_count_length MULTI_sequence_DATA_FILE -d... (5 Replies)
Discussion started by: patrick87
5 Replies

10. Shell Programming and Scripting

Perl Script : Split given month into weeks

I want to split a given month into weeks. For example if I give the date in dd/mm/yy format say 01/02/08 it should give output in the given format : week1 : start date and end date. week2 : "" week3 : "" week4 : "" (5 Replies)
Discussion started by: khushbu_roy
5 Replies
Login or Register to Ask a Question