Add column info from one file to larger second file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add column info from one file to larger second file
# 1  
Old 05-08-2012
Add column info from one file to larger second file

Hi, long time reader, first time poster.

I've done some searching so please if this is a repeated post excuse the duplicate, but what I have are two files roughly like so:

File 1:
Code:
A     W
B     X
C     Y
D     Z

File 2:
Code:
A     1
C     2
D     3

And what I would like to get out is something like:
Code:
A     1     W
B           X
C     2     Y
D     3     Z

Does anyone have a suggestion?

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data


Edited and will do in the future.

Last edited by wallysb01; 05-08-2012 at 01:09 PM..
# 2  
Old 05-08-2012
Try:
Code:
join -j1 -o1.1,2.2,1.2 -a1 <(sort file1) <(sort file2)

# 3  
Old 05-09-2012
Code:
awk '{a[$1]=a[$1]FS$2}END{for(i in a)print i,a[i]}' file2 file1 | awk 'NF < 3 {$3=FS$2;$2=FS}1'

# 4  
Old 05-09-2012
Quote:
Originally Posted by wallysb01
Hi, long time reader, first time poster.

I've done some searching so please if this is a repeated post excuse the duplicate, but what I have are two files roughly like so:

File 1:
Code:
A     W
B     X
C     Y
D     Z

File 2:
Code:
A     1
C     2
D     3

And what I would like to get out is something like:
Code:
A     1     W
B           X
C     2     Y
D     3     Z

Does anyone have a suggestion?
Code:
# awk 'BEGIN{OFS="     "}NR==FNR{a[$1]=$2}NR>FNR{if($1 in a)print $1,a[$1],$2;else{$2=OFS" "$2;print}}' file2 file1
A     1     W
B           X
C     2     Y
D     3     Z

regards
ygemici
# 5  
Old 05-09-2012
Thank you greatly, I was not familiar with the join command.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with add existing file name as new data column in new output file

Input File 1 cat S1.txt MI0043 2731 miR-1 Input File 2 cat S4.txt MI006 310 CiR-1 MI057 10 CiR-24 MI750 5 CiR-24 Desired Output File 1 cat S1.txt.out MI0043 2731 miR-1 S1.txt Desired Output File 2 cat S4.txt.out MI006 310 CiR-1 S4.txt (3 Replies)
Discussion started by: perl_beginner
3 Replies

2. Shell Programming and Scripting

Add column to file A according to file B

Hi, i have a file A like this: JAT1 JAT2 JAT3 JAT4 JAT5 JAT6 ... and file B: JAT2 1.2 JAT4 2.3 JAT6 1.5 ... (2 Replies)
Discussion started by: the_simpsons
2 Replies

3. Shell Programming and Scripting

Output Row if Third Column is Larger

Hi, I am fairly new to Unix scripting. We are running Solaris 5.10. I have the following question: Assume a text file with one text column, followed by 2 integer columns. How would I generate a script or, preferably, a command that will output the rows in which the value of the third... (6 Replies)
Discussion started by: QZ1
6 Replies

4. Shell Programming and Scripting

Creating a larger .xml file from a template(sample file)

Dear All, I have a template xml file like below. ....Some---Header....... <SignalPreference> ... <SignalName>STRING</SignalName> ... </SignalPreference> ......Some formatting text....... <SignalPreference> ......... ... (3 Replies)
Discussion started by: ks_reddy
3 Replies

5. Shell Programming and Scripting

Add new column from another file in existing file

I have two files which has one column comman in them. The two files has exact same number of rows in the same sequence. I want to add the second column of Users_detail_servicesonly.txt as last column in the existing file. 1) Users_detail_complete.txt V0135 Memb Info ... (4 Replies)
Discussion started by: Sanjeev Yadav
4 Replies

6. Shell Programming and Scripting

Script to loop line in a file and add info or do echo

I have a record.txt it will update weekly, and it could be 2 lines or more ... it just echo each line to the script san jose,23.34% tampa,2.15% dallas,30.20% seattle,44.29% Unknown,16.72% How do i write a shell script to give me a test.pl or bash file which contain #!/home/perl... (8 Replies)
Discussion started by: sabercats
8 Replies

7. Shell Programming and Scripting

Generate a mail when you add info to a txt file

Hi, I have an application's log file: /var/log/logfile which is feeded from time to time due to an application. This file contains data, what I want is: -Whenever some new data is copied to /var/log/logfileI want to generate an email to root BUT only with the new added data in the body.... (6 Replies)
Discussion started by: iga3725
6 Replies

8. UNIX for Dummies Questions & Answers

How to find a file whick is consuming larger disk space in file system

Hello, Can anybody please tell me the command to find out the filesystem or a file which is consuming larger disk space sing i want to find out the file and want to compress it please help me out any help would be appreciated (6 Replies)
Discussion started by: lokeshpashine
6 Replies

9. HP-UX

Ftp cannot put file larger than 64kb

Hi gurus, I have a problem with ftp access. The first 2 test e.g. Test A & Test B was successful with the file size 64kb (800++ numbers). The third test with file size 120kb was failed. The error is "Netout :Connection reset by peer". No password entered manually since the test run from the... (3 Replies)
Discussion started by: yeazas
3 Replies

10. Shell Programming and Scripting

Splitting a Larger File Into Mutiple Smaller ones.

Hello.. Iam in need to urgent help with the below. Have data-file with 40,567 and need to split them into multiple files with smaller line-count. Iam aware of "split" command with -l option which allows you to specify the no of lines in smaller files ,with the target file-name pattern... (1 Reply)
Discussion started by: madhubt_1982
1 Replies
Login or Register to Ask a Question