awk command - column merging


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command - column merging
# 1  
Old 08-30-2010
Question awk command - column merging

I have two files having 3 coulms and 1 column respectively
Code:
file1.txt
0    22.89    35.60
10    22.80    35.61
20    22.70    35.63
30    22.32    35.68
50    19.23    35.79
75    16.10    35.59
100    15.00    35.52
125    14.45    35.46
150    13.91    35.41
200    12.94    35.28

Code:
file 2
1575.15
1574.68
1574.16
1571.7
1550.89
1530.54
1523.9
1520.79
1517.8
1512.52
1506.69
1500.99
1491.52

required : I want to merge column 1 from file1 with coumn 1 of file2 >> new file. but in file 2 i have more rows than required.

In my new file i want equal number of rows as in file 1
(no of rows in file 1 varies everytime)
Code:
output file
0      1575.15
10    1574.68
20    1574.16
30    1571.7
50    1550.89
75    1530.54
100  1523.9
125  1520.79
150  1517.8
200  1512.52

No of rows should always be equal to number of rows in file 1 (file 2 will always have more rows than required)
# 2  
Old 08-30-2010
Try this:
Code:
awk 'NR==FNR{a[NR]=$1; next}FNR in a{print a[FNR], $0}' file1 file2

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 08-30-2010
The command works good. I have few quries regarding the command

1> what does FNR do?.
2> "next" ??
3> FNR in a ??? ->
4> can u explain the syntax used......the
# 4  
Old 08-30-2010
Quote:
Originally Posted by shashi792
The command works good. I have few quries regarding the command

1> what does FNR do?.
2> "next" ??
3> FNR in a ??? ->
4> can u explain the syntax used......the
1> what does FNR do? The FNR variable contains the number of lines read, but is reset for each file read

2> "next" ?? Read the next line and start with the first command

3> FNR in a ??? -> if the line number of the 2nd file matches with the line number of the first file

4> can u explain the syntax used......the have read of The GNU Awk User's Guide
# 5  
Old 08-30-2010
If awk seems a complicated topic , then u can try this below code
Code:
# !/usr/bin/sh

while read LINE
do
     echo $LINE | cut -d " " -f 1 >> file4.txt
done < file.txt

count=`wc -l file4.txt | cut -d " " -f 1`
paste file4.txt file1.txt | head -$count



Moderator's Comments:
Mod Comment Please use codetags, thank you

Last edited by Franklin52; 08-30-2010 at 08:46 AM..
# 6  
Old 08-30-2010
bash one-liner

Code:
paste file1 file2 | head -$(wc -l <file1)

# 7  
Old 08-30-2010
Code:
paste file1 file2  | awk '/^[0-9]/{print $1,$4}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merging rows based on same ID in First column.

Hellow, I have a tab-delimited file with 3 columns : BINPACKER.13259.1.p2 SSF48239 BINPACKER.13259.1.p2 PF13243 BINPACKER.13259.1.p2 G3DSA:1.50.10.20 BINPACKER.13259.2.p2 SSF48239 BINPACKER.13259.2.p2 PF13243 BINPACKER.13259.2.p2 G3DSA:1.50.10.20... (7 Replies)
Discussion started by: anjaliANJALI
7 Replies

2. Shell Programming and Scripting

Merging middle column in a file

I want to merge files as described below: File 1 a;1;abc b;2;def c;3;xyz d;4;pqr e;5;mno File 2 b;41 d;77 output a;1;abc (9 Replies)
Discussion started by: Prabal Ghura
9 Replies

3. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

4. Shell Programming and Scripting

Merging rows with same column 1 value

I have the following space-delimited input: 1 11.785710 117.857100 1 15 150 1 20 200 1 25 250 3 2.142855 21.428550 3 25 250 22 1.071435 10.714350 The first field is the ID number, the second field is the percentage of the total points that the person has and the third column is the number... (3 Replies)
Discussion started by: mdlloyd7
3 Replies

5. UNIX for Dummies Questions & Answers

Merging two text files by a column

So I have two text files. The first one looks like this: refsnp_id chr_name chrom_start 1 rs1000000 12 126890980 2 rs10000010 4 21618674 3 rs10000012 4 1357325 4 rs10000013 4 37225069 5 rs1000002 3 183635768 And the second one looks like this: AUC rs1000000 0.03 0.1240 AUC ... (4 Replies)
Discussion started by: evelibertine
4 Replies

6. Shell Programming and Scripting

merging two files based on first column

I had two files file1 and file2. I want a o/p file(file3) like below using first column as ref. Pls give suggestion ass join is not working as the number of lines in each file is nealry 5 C? file1 --------------------- 404000324810001 Y 404000324810004 N 404000324810008 Y 404000324810009 N... (1 Reply)
Discussion started by: p_sai_ias
1 Replies

7. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

8. UNIX for Dummies Questions & Answers

Merging Tables by a column

Dear Friends, I really do not know Linux and I really would like to understand it because it does help to work with large data. I am reading this forum for 1 week to try a solution for my problem. I think that, using others post informations, I was almost there... I have 2 big tables... (4 Replies)
Discussion started by: lColli
4 Replies

9. Shell Programming and Scripting

Extracting a column from a file and merging with other file using awk

Hi All: I have following files: File 1: <header> text... text .. text .. text .. <\header> x y z ... File 2: <header> text... text .. text .. (4 Replies)
Discussion started by: mrn006
4 Replies

10. Shell Programming and Scripting

Merging column files

Hi,Iam new to Unix.I have a file FileA which is a variable length file where each column is seperated by delimitter "|". FileA: SrNo Name Address 1-234|name1|Addr1 1-34|name2|Addr2 1-2345|name3|Addr3 FileB: SrNo Address 1-34<<06 SPACES>>Addr1<<8 spaces>> 1-234<<05... (1 Reply)
Discussion started by: swapna321
1 Replies
Login or Register to Ask a Question