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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join two lines into one, but the second line only the last two columns
# 1  
Old 05-22-2014
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.

=================
Code:
"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:

Code:
"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... Smilie

I want jump through the window....Smilie

Thanks in advance
Regards
Antonio

Last edited by vgersh99; 05-22-2014 at 06:08 PM.. Reason: code tags, PLEASE!
# 2  
Old 05-22-2014
With awk:
Code:
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

# 3  
Old 05-22-2014
Another awk solution, more verbose Smilie

Code:
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

# 4  
Old 05-23-2014
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
# 5  
Old 05-23-2014
Quote:
Originally Posted by antoniorajr
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:
Code:
(NF<3) {if ($1~/switch/) delete A; next}

to:
Code:
(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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
2 Replies

2. Shell Programming and Scripting

Join columns across multiple lines in a Text based on common column using BASH

Hello, I have a file with 2 columns ( tableName , ColumnName) delimited by a Pipe like below . File is sorted by ColumnName. Table1|Column1 Table2|Column1 Table5|Column1 Table3|Column2 Table2|Column2 Table4|Column3 Table2|Column3 Table2|Column4 Table5|Column4 Table2|Column5 From... (6 Replies)
Discussion started by: nv186000
6 Replies

3. Shell Programming and Scripting

Join common patterns in multiple lines into one line

Hi I have a file like 1 2 1 2 3 1 5 6 11 12 10 2 7 5 17 12 I would like to have an output as 1 2 3 5 6 10 7 11 12 17 any help would be highly appreciated Thanks (4 Replies)
Discussion started by: Harrisham
4 Replies

4. Shell Programming and Scripting

Join 4 files on first three columns

Hi, Can someone suggest me on how to join 4 files by comparing the first three columns? ---------- Post updated at 03:56 PM ---------- Previous update was at 03:42 PM ---------- Hope it helps someone. I was looking online for a solution and on stackoverflow, I found a solution and tried... (6 Replies)
Discussion started by: jacobs.smith
6 Replies

5. Shell Programming and Scripting

splitting a huge line of file into multiple lines with fixed number of columns

Hi, I have a huge file with a single line. But I want to break that line into lines of with each line having five columns. My file is like this: code: "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","you." I want it like this: code:... (1 Reply)
Discussion started by: rajsharma
1 Replies

6. Shell Programming and Scripting

join based on line number when one file is missing lines

I have a file that contains 87 lines, each with a set of coordinates (x & y). This file looks like: 1 200.3 -0.3 2 201.7 -0.32 ... 87 200.2 -0.314 I have another file which contains data that was taken at certain of these 87 positions. i.e.: 37 125 42 175 86 142 where the first... (1 Reply)
Discussion started by: jackiev
1 Replies

7. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

8. Shell Programming and Scripting

Join in a single line variable number of lines

Hi all, I have a file with little blocks beginning with a number 761XXXXXX, and 0, 1, 2 or 3 lines below of it beginning with STUS as follow: 761625820 STUS ACTIVE 16778294 STUS NOT ACTIVE 761157389 STUS ACTIVE 16778294 761554921 STUS ACTIVE 16778294 STUS NOT ACTIVE STUS ACTIVE OP... (4 Replies)
Discussion started by: cgkmal
4 Replies

9. Shell Programming and Scripting

join lines on line break in files

i had a file where lines appear to be broken when they shouldn't eg Line 1. kerl abc sdskd sdsjkdlsd sdsdksd \ Line 2. ksdkks sdnjs djsdjsd i can do a shift join to combine the lines but i there are plenty of files with this issue Line 1. kerl abc sdskd sdsjkdlsd sdsdksd ksdkks sdnjs... (6 Replies)
Discussion started by: mad_man12
6 Replies

10. Shell Programming and Scripting

join two lines when the second line contains "US DOLLAR"

I want to join two lines together when the second line contains "US DOLLAR". How to do that with sed? Original file: # cat testdata Sep. 24, 2005 Sep. 26, 2005 PHARMA PLUS DRUGMART 1149 $5.85 Sep. 25, 2005 Sep. 27, 2005 99 RESTAURANT #015 $11.00 US DOLLAR @ 1.203636 $13.24 ... (2 Replies)
Discussion started by: powah
2 Replies
Login or Register to Ask a Question