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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting a column from a file and merging with other file using awk
# 1  
Old 06-08-2010
Extracting a column from a file and merging with other file using awk

Hi All:
I have following files:
File 1:
Code:
<header>
text...
text ..
text ..
text ..
<\header>
         x
         y
         z
           ...


File 2:
Code:
<header>
text...
text ..
text ..
text ..
<\header>
1     2      3  
11   22     33 .....



I would like to merge these file into a single file as :

Code:
x  1   2  3
y 11  22 33 ...


into a single file named File 3.

I am kinda lost on this.
I really appreciate your help.

Last edited by Franklin52; 06-09-2010 at 03:58 AM.. Reason: Please use code tags!
# 2  
Old 06-08-2010
Code:
#!/bin/sh
awk '/header/,/\/header/{next}1' file1 > /tmp/file1.tmp
awk '/header/,/\/header/{next}1' file2 > /tmp/file2.tmp
paste -d" " /tmp/file1.tmp /tmp/file2.tmp > file3

# 3  
Old 06-08-2010
thanks bartus..I will test the script asap

Thanks!!!
I appreciate your help.
# 4  
Old 06-08-2010
Code:
awk 'NR==FNR{if(/<\\header/){f=1;next;};if(f)a[++x]=$0}{if(/<\\header/){g=1;next};if(g)print a[++z],$0}' file1 file2
x 1 2 3
y 11 22 33 .....

# 5  
Old 06-08-2010
A couple of one liners that do the same using awk or sed

paste -d" " file1 file2 | awk '/<header>/,/<\\header>/{next}1' > file3
paste -d" " file1 file2 | sed '/<header>/,/<\\header>/d' > file3
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Dummies Questions & Answers

File merging based on column patterns

Hello :) I am in this situation: Input: two tab-delimited files, `File1` and `File2`. `File2` (`$2`) has to be parsed by patterns found in `File1` (`$1`). Expected output: tab-delimited file, `File3`. `File3` has to contain the same rows as `File2`, plus the corresponding value in... (5 Replies)
Discussion started by: dovah
5 Replies

3. Shell Programming and Scripting

Extracting the column containing URL from a text file

I have the file like this: Timestamp URL Text 1331635241000 http://example.com Peoples footage at www.test.com,http://example4.com 1331635231000 http://example1.net crack the nuts http://example6.com 1331635280000 http://example2.net ... (0 Replies)
Discussion started by: csim_mohan
0 Replies

4. UNIX for Dummies Questions & Answers

How to generate one long column by merging two separate two columns in a single file?

Dear all, I have a simple question. I have a file like below (separated by tab): col1 col2 col3 col4 col5 col6 col7 21 66745 rs1234 21 rs5678 23334 0.89 21 66745 rs2334 21 rs9978 23334 0.89 21 66745 ... (4 Replies)
Discussion started by: forevertl
4 Replies

5. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

6. Shell Programming and Scripting

Help with File processing - Extracting the column

I have a line from table space report: 5 135_TT ms Normal 1774336.0 1774208.0 761152.0 1013056.0 57.1% Now I have to get 1013056.0 as o/p. For this I tried cut -f32 -d" " previously it worked now it is showing empty space. Suggest me the best code for this which... (1 Reply)
Discussion started by: karumudi7
1 Replies

7. UNIX for Dummies Questions & Answers

Extracting the last column of a text file

I would like to extract the last column of a text file but different rows of the text file have different numbers of columns. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

File merging using first column as the ref

I had two files 1.txt 2.txt. I want a 3rd file(o/p) 3.txt like below based on the common elements from the first coulmns of 1.txt and 2.txt. 1.txt 11 12 13 14 15 16 17 18 19 20 21 2.txt (6 Replies)
Discussion started by: p_sai_ias
6 Replies

9. Shell Programming and Scripting

File merging using first column as the ref

I had two files 1.txt 2.txt. I want a 3rd file(o/p) 3.txt like below (using awk) 1.txt 11 a1 12 a2 13 a3 14 a4 15 a5 16 a6 17 a7 18 a8 19 a9 20 a10 2.txt 14 b1 15 b2 16 b3 (8 Replies)
Discussion started by: p_sai_ias
8 Replies

10. Shell Programming and Scripting

Extracting 3rd column using awk from file with spaces.

BAQ001 /dev/rdsk/c2t0d7 1C13 (M) RW 69053 The line above is from a text file. I want to use awk to extract the value in the third column 1C13. I just can't seem to get the syntax right or something. Any help would be appreciated. Thanks, (5 Replies)
Discussion started by: ricnetman
5 Replies
Login or Register to Ask a Question