Help with concatinating the data of 2 files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Help with concatinating the data of 2 files

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-05-2013
ss_ss ss_ss is offline
Registered User
 
Join Date: Dec 2008
Last Activity: 18 February 2013, 1:59 PM EST
Posts: 75
Thanks: 4
Thanked 0 Times in 0 Posts
Help with concatinating the data of 2 files

Hi All,

I am trying to merge to 2 files 348.csv & 349.csv using join,awk commands but not getting proper output:


Code:
 
cat 348.csv
Timestamp              Server
                              348 
02/04/2013 09:19      100
02/04/2013 09:20      250
02/04/2013 09:21      80


Code:
 
cat 349.csv
Timestamp              Server
                              349
02/04/2013 09:19      234 
02/04/2013 09:20      13 
02/04/2013 09:21      546


Code:
 
Expected output
Timestamp                 Server      
                              348  349
02/04/2013 09:19      100  234
02/04/2013 09:20      250  13
02/04/2013 09:21      80    546


Code:
 
using below awk:
awk 'NR==FNR{i=NF<5?"__":$1$2$3$4;a[i]=$0;next} FNR==1{print}{i=NF<5?"__":$1$2$3$4}FNR>1&&i in a{print a[i],$NF}' 348.csv 349.csv


Code:
 
using below join:
join -a1 -a2 348.csv 349.csv

Thanks,
Sponsored Links
    #2  
Old 02-05-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 1 July 2014, 5:40 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Try


Code:
 
awk 'NR==FNR{A[NR]=$0;next}{ print A[FNR],FNR!=1?$NF:""}' file1 file2

The Following User Says Thank You to pamu For This Useful Post:
ss_ss (02-06-2013)
Sponsored Links
    #3  
Old 02-05-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 29 July 2014, 5:18 PM EDT
Location: Aachen, Germany
Posts: 3,923
Thanks: 62
Thanked 931 Times in 883 Posts
This one relies upon your files having exactly synchronized, corresponding lines:
Code:
awk 'NR==1 {print; getline < f1; next}
     {getline ln < f1; print ln, $NF}
    ' f1="file1" file2
Timestamp              Server
                              348  349
02/04/2013 09:19      100 234
02/04/2013 09:20      250 13
02/04/2013 09:21      80 546

You may want to alter the OFS char to make the output prettier...
The Following User Says Thank You to RudiC For This Useful Post:
ss_ss (02-06-2013)
    #4  
Old 02-06-2013
ss_ss ss_ss is offline
Registered User
 
Join Date: Dec 2008
Last Activity: 18 February 2013, 1:59 PM EST
Posts: 75
Thanks: 4
Thanked 0 Times in 0 Posts
Hi Pamu/Rudi,

Thanks for your inputs, it worked well.

But still facing some formatting issues i.e. both the numbers are coming under one server name only.

Code:
Timestamp             BRM Servers
                          348  349
02/05/2013 22:26          5 6
02/05/2013 22:36          5 6

Need the numbers below their respective server names.

Is there anyhting can be done?

Thanks in advance!!!!
Sponsored Links
    #5  
Old 02-06-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 1 July 2014, 5:40 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
do mean like this..?


Code:
 
$awk 'NR==FNR{A[NR]=$0;next}{ print A[FNR],$NF}' file1 file2

Timestamp              Server Server
                              348  349
02/04/2013 09:19      100 234
02/04/2013 09:20      250 13
02/04/2013 09:21      80 546

Sponsored Links
    #6  
Old 02-06-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 29 July 2014, 5:18 PM EDT
Location: Aachen, Germany
Posts: 3,923
Thanks: 62
Thanked 931 Times in 883 Posts
Quote:
Originally Posted by ss_ss View Post
. . .
Need the numbers below their respective server names.
Is there anyhting can be done?
. . .
Of course, but this is heavily depending on your input files. In your sample files, the server ID is far out right, with so many space before it, so don't expect it to come into the desired position by itself. As I said before, you could play around with FS and OFS variables to awk to improve the output formatting.
If you post two carefully composed input files, we can have a look onto the output formatting.
Sponsored Links
    #7  
Old 02-06-2013
ss_ss ss_ss is offline
Registered User
 
Join Date: Dec 2008
Last Activity: 18 February 2013, 1:59 PM EST
Posts: 75
Thanks: 4
Thanked 0 Times in 0 Posts
The server id is not intentionally put to the far right, got messed up while posting.

In the input file also the nos would be below the server name and same is required in the output.

Tried a lot posting a clean and formatted input output here but not able to do so, the server names are automatically shifting towards the right

Thanks
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Concatinating the lines based on number of delimiters bi.infa Shell Programming and Scripting 4 05-15-2012 01:02 PM
Problem when concatinating wildcard onto file location in bash script Lears_Fool Shell Programming and Scripting 11 09-28-2010 04:06 PM
concatinating the string in each line of the file mail2sant Shell Programming and Scripting 1 09-01-2009 04:59 PM
concatinating the array mail2sant Shell Programming and Scripting 2 05-07-2009 05:12 AM
concatinating data kingofprussia UNIX for Dummies Questions & Answers 1 05-06-2008 10:16 PM



All times are GMT -4. The time now is 06:54 PM.