Combining 2 trailer records into 1

March 4, 2010 at 06:43:43
Specs: Solaris 8
Hi, I am trying to combine 2 flat files into 1 flat file. The problem is that the 2 trailer records on the flat files contain 1 field that has a total number of records in the file. So i need to be able to combine trailer record 1 with trailer record 2 and produce trailer record 3.

i.e.
FileA
"H","GMS","NWB","APPL_APP_TYP_MST_1",2010-02-17,
"A","Y","N","N","N","N","N","","N","A","N","N","N","N","Y",
"A","Y","N","N","N","N","N","","N","A","N","Y","Y","N","N",
"T","GMS","NWB","APPL_APP_TYP_MST_1",2010-02-17,2,

FileB
"H","GMS","NWB","APPL_APP_TYP_MST_2",2010-02-17,
"R","N","N","Y","N","N","N","","N","","","","","","",
"L","N","Y","N","N","N","N","","N","","","","","","",
"T","GMS","NWB","APPL_APP_TYP_MST_2",2010-02-17,2,

FileC
"H","GMS","NWB","APPL_APP_TYP_MST",2010-02-17,
"A","Y","N","N","N","N","N","","N","A","N","N","N","N","Y",
"A","Y","N","N","N","N","N","","N","A","N","Y","Y","N","N",
"R","N","N","Y","N","N","N","","N","","","","","","",
"L","N","Y","N","N","N","N","","N","","","","","","",
"T","GMS","NWB","APPL_APP_TYP_MST",2010-02-17,4,

So its the bit creating the number 4 in trailer record for FileC which is the proble.

Thanks in advance


See More: Combining 2 trailer records into 1

Report •

#1
March 4, 2010 at 10:11:38
First, let's talk about what constitues a record. Is the record 1 line long? The reason I ask is that as you've posted, FileA and FileB each are 4 lines long.

I am assuming that you mean each record is one line. Here are two ways:

# UNTESTED echo the last line
# using tail

tail -1 FileA > FileC
tail -1 FileB >> FileC

# using sed
sed -n '$p' fileA > FileC
sed -n '$p' fileB >> FileC


Report •

#2
March 5, 2010 at 00:52:56
Hi,

Thank you very much for your help.

The problem i am having is specifically with the trailer records which are 1 line long (sorry should have said this originally)

"T","GMS","NWB","APPL_APP_TYP_MST_1",2010-02-17,2,
"T","GMS","NWB","APPL_APP_TYP_MST_2",2010-02-17,2,

I need to merge these together to be the following 1 line
"T","GMS","NWB","APPL_APP_TYP_MST",2010-02-17,4,

i.e amend the name of the 4th column/character by dropping the _1 and _2.
And also cobine / sum up the values in the 6th column i.e. 2 + 2 = 4.

Hope this makes sense


Report •

#3
March 5, 2010 at 23:08:25
This nawk script should do what you want. The output is sent to fileC.new

#!/bin/ksh

nawk ' BEGIN { FS=","; OFS=","; fqty=0 }
{
# add cumulative field 6 total
fqty=fqty + $6
$6=fqty
# elimate _1 & _2 from field 4
gsub("_1","",$4)
gsub("_2","",$4)
row=$0
}
END {
# at the end, print the row
    print row
} ' fileC > fileC.new


Report •
Related Solutions


Ask Question