The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Please Help:Need to Split the file into mutliple files depends on the KEY field value arund_01 UNIX for Dummies Questions & Answers 14 04-23-2008 04:42 PM
Split file into multiple files depending upon first 4 digits deepakgang Shell Programming and Scripting 4 04-09-2008 02:21 AM
Help Needed : Split one big file to multiple files monicasgupta Shell Programming and Scripting 5 03-03-2008 07:09 PM
Split A File Into 2 Files dummy_needhelp Shell Programming and Scripting 7 11-04-2007 07:36 PM
Split a file into 2 or more files bobo UNIX for Dummies Questions & Answers 4 01-16-2006 05:15 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-02-2007
eltinator eltinator is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 63
Split File into seperate files

Hi,

So I have a text file which I want to separate into separate text files. I would use the split command but the problem here is that the text file is separated by delimiters. For example:

blah
blah blah
------
more text
-----
and some more text

So basically the first part should be one text file and then the middle part (more text) should be its own text file and finally the bottom another one. I was thinking of writing a while loop to do this but I'm a newb to shell coding so maybe someone could help me fill in the blanks:

Code:
# Set line to beginning of file
$line = #not sure how to do this

# Shell to save everything above first delimiter in header
while ($line != "-----")
do
        cat file $line >> temp_file
        mv temp_file file
       # increment $line to next line
done
And repeat for the other 2 parts. The original file should really only have 3 parts, maybe 4, so I could just repeat the loop but if someone wants to make this efficient be my guest. Any help would be much appreciated. Thanks so much!

Elt

Last edited by eltinator; 08-02-2007 at 06:42 PM..
  #2 (permalink)  
Old 08-02-2007
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
I'd use csplit for this. The following script demonstrates how you can apply it

Code:
$ cat ./split_it.sh 
#!/bin/bash

rm -f split_file part.??

cat >split_file <<EOF
a
b
c
-----
d
e
f
-----
g
h
i
-----
j
k
l
EOF

csplit -k -s -f part. split_file /^-----/ "{100}" 2>/dev/null

ls part.?? | while read file; do
   sed '/^-----/ d' ${file} > ${file}.new && mv ${file}.new ${file}
   echo "File ${file} contains:"
   cat ${file}
done

exit 0
$ ./split_it.sh 
File part.00 contains:
a
b
c
File part.01 contains:
d
e
f
File part.02 contains:
g
h
i
File part.03 contains:
j
k
l
Cheers,
ZB
  #3 (permalink)  
Old 08-02-2007
eltinator eltinator is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 63
Cool! This works really nicely, thanks so much =D
  #4 (permalink)  
Old 08-03-2007
eltinator eltinator is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 63
Sorry, one more quick question. If I wanted to keep the delimiter in the separate files, how would that change the code. For example if my example is:

header
-----
body
-----
footer


The separate files should have the text and then delimiter following it with the exception of the last file. So the first file would be:


header
------


Thanks!
  #5 (permalink)  
Old 08-03-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
To keep the delimiter in each file, just remove the 'sed':
Code:
sed '/^-----/ d' ${file} > ${file}.new && mv ${file}.new ${file}
And the code will be:
Code:
csplit -k -s -f part. split_file /^-----/ "{100}" 2>/dev/null
ls part.?? | while read file; do
   echo "File ${file} contains:"
   cat ${file}
done
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 01:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0