Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-02-2008
Registered User
 

Join Date: Aug 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Split a file based on a pattern

Dear all,

I have a large file which is composed of 8000 frames, what i would like to do is split the file into 8000 single files names file.pdb.1, file.pdb.2 etc etc

each frame in the large file is seperated by a "ENDMDL" flag so my thinking is to use this flag a a point to split the files up. however im having great dificulty in trying to output each frame to a new file in perl? suggetsions and help will be much appreciated.

example of the large file


Code:
ENDMDL
mpla
mpla ok ok ,pla mpla mpla
mpla 
mpla
ENDMDL
mpla2 mpla2 mpla2 mpla2
mpla2 mpla2 
mpla2 mpla2
ENDMDL
mpla
 mpla ok ok ,pla mpla mpla
 mpla 
 mpla
 ENDMDL
 mpla2 mpla2 mpla2 mpla2
 mpla2 mpla2 
 mpla2 mpla2
 ENDMDL

MISH
Sponsored Links
  #2  
Old 12-02-2008
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 5,591
Thanks: 2
Thanked 68 Times in 65 Posts
Try this:


Code:
awk '/ENDMDL/{i++}{print > "file.pdb."i}' file

  #3  
Old 12-02-2008
Registered User
 

Join Date: Dec 2008
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Cool...
  #4  
Old 12-02-2008
Registered User
 

Join Date: Aug 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Franklin52,
This works really well and its pretty quick.

Mish
  #5  
Old 12-02-2008
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,165
Thanks: 0
Thanked 1 Time in 1 Post
you can use below to set 'ENADMDL' as the line seperator instead of use the default '\n'


Code:
$/="ENDMDL";

Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Split File Based on Line Number Pattern shankster Shell Programming and Scripting 11 10-01-2008 09:49 AM
how to change a particular value in a file based on a pattern orbeyen UNIX for Dummies Questions & Answers 9 08-20-2008 03:09 AM
Split a file based on pattern in awk, grep, sed or perl kumarn Shell Programming and Scripting 5 06-20-2008 10:51 AM
Split a file with no pattern -- Split, Csplit, Awk madhunk UNIX for Dummies Questions & Answers 10 12-17-2007 11:57 AM
awk script to split a file based on the condition superprogrammer Shell Programming and Scripting 12 06-14-2005 03:59 AM



All times are GMT -4. The time now is 08:00 AM.