Merging columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging columns
# 1  
Old 06-20-2008
Hammer & Screwdriver Merging columns

Hi, I have input file.

File1:

Seqno Name
124 name1
121 name2
123 name3
122 name4

We will send the file1 to some other team. They will replace name column with place in file1 and send back to us as file2.

file2:

Seqno Place

124 place1
121 place2
123 place3file2:

And then we will replace the name with place based on row-id and output file should only contain the row-id and place as shown below.

Output:

File1:

Row-id Place
121 place2
122
123 place3
124 place1


Please help me how to do it. Thanks in advance.
# 2  
Old 06-20-2008
Try this:

Code:
awk 'NR==FNR{a[$1]=$2;next}{print $1 " " a[$1]}' file2 file1

Regards
# 3  
Old 06-20-2008
Thanks for reply. But when I gave the below command.

awk 'NR==FNR{a[$1]=$2;next}{print $1 " " a[$1]}' file2 file1

The output file is as shown below :

124
121
123
124
121
123
122

I think we have to sort the files and give the above command. Please correct me if Iam wrong.

Last edited by manneni prakash; 06-20-2008 at 05:55 AM..
# 4  
Old 06-20-2008
Code:
awk 'NR==FNR{a[$1]=$2;next}{print $1 " " a[$1]}' file2 file1

Works fine for me with the given files.....I get this output:

Code:
124 place1
121 place2
123 place3
122

# 5  
Old 06-23-2008
Quote:
Originally Posted by manneni prakash
Thanks for reply. But when I gave the below command.

awk 'NR==FNR{a[$1]=$2;next}{print $1 " " a[$1]}' file2 file1

The output file is as shown below :

124
121
123
124
121
123
122

I think we have to sort the files and give the above command. Please correct me if Iam wrong.
You should use nawk or /usr/xpg4/bin/awk on Solaris.
# 6  
Old 06-23-2008
Thanks

Thanks a lot...... I have given gawk instead of awk its working now........ Thanks again.....................
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging and Adding colon to columns

Hello, I have a tab delim file that looks like this CHROM POS ID REF ALT ID HGVS_C HGVS_P 1 17319011 rs2076603 G A NM_022089.3,NM_001141973.2,NM_001141974.2 c.1815C>T,c.1800C>T,c.1800C>T p.Pro605Pro,p.Pro600Pro,p.Pro600Pro 1 20960230 rs45530340 ... (3 Replies)
Discussion started by: nans
3 Replies

2. Shell Programming and Scripting

Merging Columns

Hi, Can you please help me. I have 2 files to merge File1 1251 743 1250 742 1249 741 1248 749 1247 722 1246 740 1245 739 1244 740 1243 705 1242 631 1241 590 File2 (2 Replies)
Discussion started by: jiam912
2 Replies

3. Shell Programming and Scripting

Merging Multiple Columns between two files

Hello guys, I have 2 CSV files which goes like this: CSV1: Breaking.csv: UTF-8 "Name","Description","Occupation","Email" "Walter White","","Chemistry Teacher","w.w@bb.com" "Jessie Pinkman","","Junkie","j.p@bb.com" "Hank Schrader","","DEA Agent","h.s@bb.com" CSV2: Bad.csv... (7 Replies)
Discussion started by: jeffreybsu
7 Replies

4. Shell Programming and Scripting

Merging two columns into one

Suppose I have file1.txt 1 2 4 5 10 11 and I want to produce 1 2 4 5 10 11 file2.txt Thanks for your help :) (2 Replies)
Discussion started by: johnkim0806
2 Replies

5. UNIX for Dummies Questions & Answers

Merging two text files by two columns

Hi, I have two text files that I would like to merge/join. I would like to join them if the first columns of both text files match and the second column of the first text file matches the third column of the second text file. Example input: First file: 1334 10 0 0 1 5.2 1334 12 0 0 1 4.5... (4 Replies)
Discussion started by: evelibertine
4 Replies

6. Shell Programming and Scripting

Merging non-repeating columns of lines

Hello, I have file to work with. It has 5 columns. The first three, altogether, constitutes the position. The 4th column contains some values for downstream analysis and the fifth column contains some values that I want to add to 4th column (only if they happen to be in the same position). My... (5 Replies)
Discussion started by: menenuh
5 Replies

7. Shell Programming and Scripting

Merging columns from multiple files

Hello, I have a number of tab delimited data files consists of two columns. Like that: File1 800.000000 0.002744 799.000000 0.002517 798.000000 0.002836 797.000000 0.002553 FIle2 800.000000 0.000261 799.000000 0.000001 798.000000 0.000551 797.000000 0.000275 File3... (19 Replies)
Discussion started by: erden
19 Replies

8. UNIX for Dummies Questions & Answers

Merging two columns

Hi, I have two columns that look like this (tab seperated): name top carl ball bob lost joe smith I want the two columns to merge and look like this: nametop carlball boblost joesmith Also, I want to trim the edges of a column. So lets say the above column... (3 Replies)
Discussion started by: phil_heath
3 Replies

9. UNIX for Dummies Questions & Answers

Merging Non-Empty Columns within a CSV

I am trying to place all my data in a single row (order doesn't matter). Note I am a Unix novice, so please go easy on me. Here is an example Raw data: row# (1) 45 64 23 (2) 32 1 6 56 (3) 32 45 Needs to be like this: row# (1) 45 (2) 32 (3) 32 ... (2 Replies)
Discussion started by: mmann1123
2 Replies

10. UNIX for Dummies Questions & Answers

Extracting columns from different files for later merging

Hello! I wan't to extract columns from two files and later combine them for plotting with gnuplot. If the files file1 and file2 look like: fiile1: a, 0.62,x b, 0.61,x file2: a, 0.43,x b, 0,49,x The desired output is a 0.62 0.62 b 0.61 0.49 Thank you in advance! (2 Replies)
Discussion started by: kingkong
2 Replies
Login or Register to Ask a Question