Splitting File into Multiple with conditions

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Splitting File into Multiple with conditions

stsana123
Hi ,

  I have a situation where I need to split a big file,

say file name    
test_ddmmhh.001 into multiple files like test_ddmmhh_1.001,test_ddmmhh_2.001 etc

The file contents are  test_ddmmhh.001

FHEAD 1111  
BHEAD   1
BTAIL    2
BHEAD   7
BTAIL    10
BHEAD   9
BTAIL    3
BHEAD   8
BTAIL    3
FTAIL 8(COUNT OF DETAIL LINES).

I want to split this file at BTAIL preserving the HEADER AND TRAILER(with correct detail count) like this
which BTAIL I need to cut is dynamic based on the record count, say If I say count=4 then it should be as follows.

 test_ddmmhh_1.001
--------------------
FHEAD 1111  
BHEAD   1
BTAIL    2
BHEAD   7
BTAIL    10
FTAIL 4(COUNT OF DETAIL LINES).

 test_ddmmhh_1.001
--------------------
FHEAD 1111  
BHEAD   9
BTAIL    3
BHEAD   8
BTAIL    3
FTAIL 4(COUNT OF DETAIL LINES).

I am new to the unix scripting, and any help will be greatly appreciated.

Thanks,
San.
Reply | Threaded
Open this post in threaded view
|

Re: Splitting File into Multiple with conditions

Guru
Administrator
Script : split.sh <filename>
Pass the filename as argument to the script:


#!/bin/bash

FILE="f1.txt"
read header <$FILE
trailer=$(tail -1 $FILE | awk '{print $1}')
cnt=$(wc -l < $FILE)
file=$(echo $FILE | sed 's/\..*//' )
ext=$(echo $FILE | sed 's/.*\.//' )

awk -v cnt=$cnt 'NR!=1 && NR!=cnt' $FILE | awk -v hdr="$header" -v
trl="$trailer" -v file=$file -v ext=$ext '
    NR%4==1 {
        if (cnt){
            print trl"  "cnt > f;
        }
        f=file""++i"."ext;
        cnt=0;
        print hdr > f;

    }{
    print > f;
    cnt++;
}END{
    print trl"  "cnt > f;
}
    '