Split File into seperate files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split File into seperate files
# 1  
Old 08-02-2007
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  
Old 08-02-2007
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  
Old 08-02-2007
Cool! This works really nicely, thanks so much =D
# 4  
Old 08-03-2007
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  
Old 08-03-2007
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

For loop for seperate files

For shell script. If I had two separate files, file.txt and file1.txt and each has just a list of names from the who command. How would I create an if loop to compare each name? (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

Split a file into 10 different files

OS : RHEL 6.7 Shell : bash I have a text file with 5.97 million lines. I want to split this big file into 12 different files (in sequential order) so that each file will contain roughly 500K lines. I tried the following awk command after googling. But, it just created 2 files... (5 Replies)
Discussion started by: omega3
5 Replies

3. Shell Programming and Scripting

Seperate Odd and Even numbers from 1 file to 2 files

Hey guys. I have been trying to figure out an easy way to seperate a liste of 150k numbers (10 digits) in a .txt file into odd and even numbers with each of their own files, for a project at work. I've tried Excel, but it was too much for it and it wasnt very simple. So i gave up after... (13 Replies)
Discussion started by: TranceC
13 Replies

4. UNIX for Dummies Questions & Answers

looping through file and creating seperate files

Hi, My first post!! I have a files with header, something like this Header_Row AMC|D1|D2|D2 AAO|D3|D4|D5 AMC|D6|D7|D8 AAO|D9|D10|D11 . . . . . and millions fo records thereafter like this. I want to read the above file in a loop and write the lines having AMC into another... (1 Reply)
Discussion started by: amitbakre
1 Replies

5. Shell Programming and Scripting

split a file into many files

Hello, Here is another one. The file type is almost same, many lines and many fields. What I need to do is to extract each line of old file and make it a new file, and in the new file, the field1 will be file name and the rest of field will be transpose to line. Say, 1, field1 field2 ... (8 Replies)
Discussion started by: ssshen
8 Replies

6. Shell Programming and Scripting

split a sentence and seperate into two lines

Hi, I have a string as str="route net,-hopcount,1,255.255.255.0,10.230.20.111,10.230.20.234 Route True route net,-hopcount,0,-netmask,255.255.248.0,0,10.230.23.254 Route True" I need to split this string into two lines as route net,-hopcount,1,255.255.255.0,10.230.20.111,10.230.20.234... (4 Replies)
Discussion started by: chaitanyapn
4 Replies

7. UNIX for Dummies Questions & Answers

comparing strings in seperate files

Hello, I am comparing files with for mismatches using fgrep but I've run into a problem. fgrep -vf $file1 $file2 > mismatches.dat file1 and file2 both contain file names on each line file1 has filenames which are up to 92 characters long and contain the "$" char. example file name:... (2 Replies)
Discussion started by: orahi001
2 Replies

8. Shell Programming and Scripting

Split A File Into 2 Files

i WANT TO SPLIT A FILE WHICH HAS 250 COLUMNS. and the delimiter is '|'. So , can somebody help me with the command i have to use to split the file into two files. thanks (7 Replies)
Discussion started by: dummy_needhelp
7 Replies

9. Shell Programming and Scripting

Email like files in seperate emails

My goal is to send multiple files to a person based on their input. The files have similar names like: file1-egress-filter file2-ingress-filter stuff1-egress-filter stuff2-ingress-filter ... The script is run with the filename given as arguments, such as: ./mail.sh file stuff would... (6 Replies)
Discussion started by: earnstaf
6 Replies

10. UNIX for Dummies Questions & Answers

Split a file into 2 or more files

Dear friends: I have a datafile contains 1 to 40 lines or i can be varied between 1 to 40. I want to split the datafile into smaller files: if the datafile has 40 lines or more, file1 contains line 1 to 12 file2 contains line 13 to 25 file3 contains line 26 to 28 file4 contains line 29... (4 Replies)
Discussion started by: bobo
4 Replies
Login or Register to Ask a Question