![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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
Elt Last edited by eltinator; 08-02-2007 at 06:42 PM.. |
|
||||
|
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! |
|
|||||
|
To keep the delimiter in each file, just remove the 'sed':
Code:
sed '/^-----/ d' ${file} > ${file}.new && mv ${file}.new ${file}
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|