The UNIX and Linux Forums  


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




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 11-20-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 712
Hi.

Command csplit was designed for this:

Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate context split, csplit.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash csplit

echo

# Remove debris files.
rm -f xx*

FILE=${1-data1}

csplit --keep-files -z $FILE "/Company Code/-1" {*}

echo
echo " Files created:"
ls xx*

SAMPLE=xx01
echo
echo " Sample $SAMPLE:"
cat -n $SAMPLE

exit 0

Producing:

Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
csplit (coreutils) 5.2.1

1
379
218
81

 Files created:
xx00  xx01  xx02  xx03

 Sample xx01:
     1  KOKRS EL01 RLDNR M2 RRCTY 1
     2  Company Code 100
     3  Fiscal Year 2007
     4  Version PW3
     5  Currency USD
     6  1 2 3 4
     7  1 2 3 4
     8  BA Account number Profit Ctr MRA Jan-TC Feb-TC
     9  A 93010000 1530 152 1429793
    10  A 93010000 9999 403 0 0 0
    11  A 93010000 9999 404 -142
    12  A 93010000 9999 411 0 0 0
    13  A 93010000 9999 465 214538 214538 6114330
    14  A 93010000 9999 487 0 -207918
    15  A 93010000 471 502 0 0 0
    16  A 93010000 9999 502 0 0 0
    17

See man csplit for details ... cheers, drl