Perl break a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl break a file
# 1  
Old 08-31-2010
Perl break a file

I am trying to break a large file into smaller ones, with the break defined by the character "*".

The following is the code I have so far which breaks the input file on every line instead of at the "*" character.

Input file:
Code:
1
2
3
4
*5
6
7
8
9

Code:
Code:
#!/usr/bin/perl
open(FILE,"j1");
open(OUT,"lds_1.txt");
$filenum=1;
while(<FILE>) {
     if(  ~ /^\*/) {
           $filenum++;
     open(OUT1,">lds_$filenum.txt") || die $!;
     print OUT1 $_;
                   }
print OUT $_;
               }
close(FILE);

The output should be two files with the first file containing 1-4 and the second 6-9. The code above creates 9 files with a number in every file.

Any help is greatly appreciated.
Thanks
# 2  
Old 08-31-2010
Code:
perl -0052ne 'open O, ">lds_$.";print O $_' j1

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 09-01-2010
Very efficient code bartus11.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Break output file into three files

Help! :) I am getting an output file that looks similar to below. EMAIL_ADDR ----------------------------------------------------------------------------------- user@gmail.com DATABASENAME ----------------------------------------------------------------------------------- db1 db2 db3... (6 Replies)
Discussion started by: cpolikowsky
6 Replies

2. Shell Programming and Scripting

Break a large file

Hi Friends, I have the following file and I would like to split it after every line that starts with done The file is like this cat script #!/bin/bash # # Name: name # Task: name # #$ -N name #$ -S /bin/bash #$ -m be #$ -M xxx #$ -e xxx #$ -o xxx (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. UNIX for Dummies Questions & Answers

add a string to a file without line break

I searched and found "echo -n" and "printf" are solution for this, but they are not here: $ echo "hello" >> test $ cat test hello $ echo -n "world" >> test $ cat test hello world $ echo -n " seriously?" >> test $ cat test hello world seriously? This is not successful... (15 Replies)
Discussion started by: stunn3r
15 Replies

4. Shell Programming and Scripting

Page Break in a file for printing

Hi, We have 1lac records in source file and unix script will genarate around 1000 files. From target location the files are taking for printing on physical papers. the page size limitation : 256 Lines Can you please tell me how to insert the page break in a flat file for printer. (5 Replies)
Discussion started by: koti_rama
5 Replies

5. Shell Programming and Scripting

Page Break in large file

Hi, The shell script inserting the millions of rows into target flat file system and handling the line number for each line. We need a page break line after every 10,000 lines. is there any command to insert a page break line into target file. (3 Replies)
Discussion started by: koti_rama
3 Replies

6. Shell Programming and Scripting

how do I break line in a file when a pattern is matched ?

Hi All, I am stuck for quite sometime now. Below is a line in my file - GS|ED|001075|001081|20110626|1806|100803|X|004010ST|130|100803001 This line occurs only once and it is the second line. I have to break this line into two lines from ST (bold) such that it looks like -... (5 Replies)
Discussion started by: ihussain
5 Replies

7. UNIX for Dummies Questions & Answers

to break a file into 2 files after matching a pattern.

Hi, i need to break a file into 2 files afetr matching a pattern for ex. there is a fil, file .txt which contains here i need to look for mat $ demon if it matches then i need to transfer the data into another file till the line in which a "d6s" comes,and i have to delete tat line... (3 Replies)
Discussion started by: manit
3 Replies

8. Shell Programming and Scripting

Perl - How do you break the long line of codes into 2?

I'm trying to make this long line of codes in Perl looks nice by dividing it into 2 lines... Before: `echo "blahblahblahblahblahblahblahblahblahblahblah" > ~/lalala/lalala/lalala/lalala/lalala/abc`; After: `echo "blahblahblahblahblahblahblahblahblahblahblah" > ... (5 Replies)
Discussion started by: teiji
5 Replies

9. Shell Programming and Scripting

Break a file into separate files

Hello I am facing a scenario where I have a file with XML content and I am running shell script over it. But the problem is the XML is getting updated with new services. In the below scenario, my script takes values from the xml file from one service name say ABCD. Since there are multiple, it is... (8 Replies)
Discussion started by: chiru_h
8 Replies

10. Shell Programming and Scripting

how to break mysql dump sql file

Hi folks I have mysql dump which having insert queries, i want to break that file when 10 complete "INSERTS" lines so extract that line and store in 1.sql and 2.sql and for next 10 insert lines. pls guide me how can i do that. Regards, Bash (2 Replies)
Discussion started by: learnbash
2 Replies
Login or Register to Ask a Question