Computing.Net > Forums > Unix > split a file with headers and trail

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

split a file with headers and trail

Reply to Message Icon

Name: Pradeepa
Date: October 23, 2008 at 03:39:23 Pacific
OS: UNIX
CPU/Ram: 1 GB
Comment:

Hi,

I will have to split a 4 MB file into four 1 MB files. But the problem here is the first line is a header and the last line is a trailer. In between I have second level header which can repeat (2HDR) and under each 2HDR there can be any number of other lines and a second level trailer (2TRL).When the file is split, and one 2HDR should have its 2TRL in the same file. One set can be as small as 200KB and as big as 1 MB.

sample file

HDR
2HDR
...........
..........
.........
2TRL
2HDR
.........
.........
........
2TRL
2HDR
.........
2TRL
TRL
Thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: October 23, 2008 at 12:36:32 Pacific
Reply:

A way ... But whatever method you choose, it will probably take a while.

I'm assuming you want the HDR and TRL lines removed. This will break datafile into splitfile.1, splitfile.2 and splitfile.3.

Change the filename when you find a line starting with 2HDR.



#!/bin/ksh

rm -f splitfile.*
fbase="splitfile"
cnt=0
while read line
do
case $line in
HDR|TRL)
continue
;;
2HDR*)
((cnt+=1))
ff="${fbase}.${cnt}"
;;
esac
echo "$line" >> $ff
done < datafile



0

Response Number 2
Name: Pradeepa
Date: November 6, 2008 at 20:49:29 Pacific
Reply:

Thanks a million. It worked.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: split a file with headers and trail

Create a file with a path? www.computing.net/answers/unix/create-a-file-with-a-path/4820.html

Split a file into 2 files www.computing.net/answers/unix/split-a-file-into-2-files/7676.html

How would I change an entry to a file with a shell script? www.computing.net/answers/unix/how-would-i-change-an-entry-to-a-file-with-a-shell-script/1869.html