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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting and recreating files 1

Status
Not open for further replies.

dayankoven

Programmer
Oct 28, 2002
17
MY
Fellow gurus,

I have the following situation. One flat file needs to be splitted into 2 based on a certain condition. The file come with the following format :-

A BWCIF abc 02022004061853
Drec1 abc abc abc abc abc cdef A
Drec2 abc abc abc abc abc cdef I
Drec3 abc abc abc abc abc cdef A
Drec4 abc abc abc abc abc cdef I
Drec5 abc abc abc abc abc cdef A
Drec6 abc abc abc abc abc cdef I
Drec7 abc abc abc abc abc cdef A
Drec8 abc abc abc abc abc cdef I
Drec9 abc abc abc abc abc cdef A
Drec10 abc abc abc abc abc cdef I
Drec11 abc abc abc abc abc cdef A
T 00011

I need to break this file into the following format :-

File A

A BWCIF abc 02022004061853
Drec1 abc abc abc abc abc cdef A
Drec3 abc abc abc abc abc cdef A
Drec5 abc abc abc abc abc cdef A
Drec7 abc abc abc abc abc cdef A
Drec9 abc abc abc abc abc cdef A
Drec11 abc abc abc abc abc cdef A
T 00006

File B

A BWCIF abc 02022004061853
Drec2 abc abc abc abc abc cdef I
Drec4 abc abc abc abc abc cdef I
Drec6 abc abc abc abc abc cdef I
Drec8 abc abc abc abc abc cdef I
Drec10 abc abc abc abc abc cdef I
T 00005

I have the following syntax to help split the file :-

cat abc| awk 'substr($1,1,1)~/A/ {print $0}' > A_file
cat abc| awk 'substr($NF,length($NF),1)~/A/ {print $0}' >> A_file
cat abc| awk 'substr($1,1,1)~/T/ {print $0}' >> A_file

cat abc| awk 'substr($1,1,1)~/A/ {print $0}' > I_file
cat abc| awk 'substr($NF,length($NF),1)~/I/ {print $0}' >> I_file
cat abc| awk 'substr($1,1,1)~/T/ {print $0}' >> I_file

The problem i have now is that i don't know how to re-create the trailer record(T) based on the new record count.It should show 5 and 6 respectively for the I file and the A file respectively. The syntax above just shows the old record count which is 11. Can someone please help me with this.

Thanks in advance for all the help.
 
Try this.

awk -f dayan.awk infile

# --- dayan.awk ---
NR==1{print > "A_file";print > "I_file";next}
/A$/{print > "A_file";n1++;next}
/I$/{print > "I_file";n2++;next}
/^T/{
printf("T %05d\n",n1)>"A_file"
printf("T %05d\n",n2)>"I_file"
exit
}

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top