Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split on unix file to two files 1

Status
Not open for further replies.

alhassani54

Programmer
Aug 1, 2002
27
0
0
GB
Hi

I have a file which contain one line of data
For example
UNB+UNOA:2+FPC1+FPA1+061026:0747+00000002++FINFH+++UNKNOWN'
UNH+00000002+FINFH:0:3:FH'BGM+++243:200610260047:306+505'UN
T+3+00000002'UNZ+1+00000002'UNB+UNOA:2+FPC1+FPA1+061024:124
1+00000043++FINFH+++UNKNOWN'UNH+00000046+FINFH:0:3:FH'BGM++
+243:200610241241:306+505'UNT+3+00000046'UNZ+1+00000043'

I would like to split the file to two files
File one:-
UNB+UNOA:2+FPC1+FPA1+061026:0747+00000002++FINFH+++UNKNOWN'
UNH+00000002+FINFH:0:3:FH'BGM+++243:200610260047:306+505'UN
T+3+00000002'UNZ+1+00000002’

File two:-
UNB+UNOA:2+FPC1+FPA1+061024:1241+00000043++FINFH+++UNKNOWN'
UNH+00000046+FINFH:0:3:FH'BGM+++243:200610241241:306+505'UN
T+3+00000046'UNZ+1+00000043’
 
For the exact line you posted:

Code:
#!/bin/ksh

full_line=`cat file_you_have`

first_part=${full_line%UNB*}
second_part=${full_line#${first_part}}

echo $first_part > file_one
echo $second_part > file_two

- Rod


IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

Wish you could view posts with a fixed font? Got Firefox & Greasemonkey? Give yourself the option.
 
Something like split -n 146 ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
OOps, sorry for the typo.
split -b 146

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

Or if your file has multiple lines:
Code:
awk '{i=index(substr($0,2),"UNB+")
print substr($0,1,i);print substr($0,i+1)}' MyFile|\
split -l 1 - MyFile
[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top