Join two lines into one, but the second line only the last two columns

Hi guys,

I hope you are doing well!

I have a file and I need to join two lines into one, but the second line I need only the last two columns.

=================

"eHealth Trend Report","logoRpt"
"LAN/WAN Group 123"
"Divide by Time"

"switch1_a-RH-Serial0"
"BW: 1.02 M"


"Time","Duration","Bits In /sec","Bits Out /sec","Bytes In /sec","Bytes Out /sec","Unicast In /sec","Unicast Out /sec"
"22-May-14 11:26:28",301,28327.54817276,5598.72425249,3540.94352159,699.84053156,10.97009967,9.57807309
"22-May-14 11:31:29",306,41478.66666667,7049.75163399,5184.83333333,881.21895425,17.91503268,13.27450980
"22-May-14 11:36:35",294,19973.52380952,6858.28571429,2496.69047619,857.28571429,18.00340136,13.20068027
"22-May-14 11:41:29",299,46029.08361204,8266.38127090,5753.63545151,1033.29765886,21.39130435,15.98996656
"Time","Duration","Bandwidth Utilization In %","Bandwidth Utilization Out %"
"22-May-14 11:26:28",301,2.76636209,0.54675040
"22-May-14 11:31:29",306,4.05065120,0.68845233
"22-May-14 11:36:35",294,1.95053952,0.66975444
"22-May-14 11:41:29",299,4.49502778,0.80726379


"switch2_a-GigabitEthernet2/0/0"
"BW: 155.0 M"

"Time","Duration","Bits In /sec","Bits Out /sec","Bytes In /sec","Bytes Out /sec","Unicast In /sec","Unicast Out /sec"
"22-May-14 11:26:28",301,25000492.22591362,108868761.08970100,3125061.52823920,13608595.13621262,9294.19269103,13032.61794020
"22-May-14 11:31:29",306,23421045.12418301,104744759.21568628,2927630.64052288,13093094.90196078,9040.73202614,12560.07516340
"22-May-14 11:36:35",294,27179805.60544218,118195109.44217686,3397475.70068027,14774388.68027211,10144.23469388,13957.22108844
"22-May-14 11:41:29",299,26196457.73913043,120163420.46822743,3274557.21739130,15020427.55852843,10038.54180602,14001.65551839
"Time","Duration","Bandwidth Utilization In %","Bandwidth Utilization Out %"
"22-May-14 11:26:28",301,16.12934911,70.23791139
"22-May-14 11:31:29",306,15.11035156,67.57726333
"22-May-14 11:36:35",294,17.53535887,76.25490939
"22-May-14 11:41:29",299,16.90094096,77.52478313

=============

The output is:

"22-May-14 11:26:28",301,28327.54817276,5598.72425249,3540.94352159,699.84053156,10.97009967,9.57807309,2.76636209,0.54675040
"22-May-14 11:31:29",306,41478.66666667,7049.75163399,5184.83333333,881.21895425,17.91503268,13.27450980,4.05065120,0.68845233
"22-May-14 11:36:35",294,19973.52380952,6858.28571429,2496.69047619,857.28571429,18.00340136,13.20068027,1.95053952,0.66975444
"22-May-14 11:41:29",299,46029.08361204,8266.38127090,5753.63545151,1033.29765886,21.39130435,15.98996656,4.49502778,0.80726379
.
.
.
.

I tried use while and case but didn't work. After I tried to use awk, but didn't work... :mad:

I want jump through the window....:confused:

Thanks in advance
Regards
Antonio

With awk:

awk '
BEGIN {FS=OFS=","}
(NF<3) {if ($1~/switch/) delete A; next}
{key=$1 FS $2}
(key in A) {print A[key],$(NF-1),$NF; next}
{A[key]=$0}
' file

Another awk solution, more verbose :smiley:

awk -F"," 'BEGIN{f=1}
NR==FNR {
    if (NF==0 && b==4) {
        f=0
    }
{$0 ~ /"[0-9]/ && f>0 && NF==8 && ln[i++]=$0}
{b=NF}
next
}
{
    if ($0 ~ /"[0-9]/ && NF==4) {
    nln[j++]=","$(NF-1)","$NF
    f++}
    if (NF==0 && f>0) {
    exit
    }
}
END{
for (x=0;x<j;x++) 
print ln[x] nln[x]
}' file file

Hi MadeInGermany

I'm still learning awk...
I tried to print the switch name, but I didn't do...
Is it possible to get "switch2_a-GigabitEthernet2/0/0" and print before the block?

what the variable that awk store this information?

Thanks in advance
Best Regards
Antonio

You didn't say you wanted those lines (or the headers) to be printed, but if you change the following line in MadeInGermany's script:

(NF<3) {if ($1~/switch/) delete A; next}

to:

(NF<3) {if ($1~/switch/) {print;delete A}; next}

it should print those lines for you if his script did what you wanted other than that. The delete array statement is not required by the standards, but is provided as an extension in many implementations of awk.