Merge column file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge column file
# 1  
Old 01-12-2015
Linux Merge column file

Hi All,

I have on file1 and file2 some,
Code:
$cat file1
aaa     
bbb     
ccc
ddd    
eee 
fff
     
ggg    
hhh    
iii
jjj

with line blank,

and
Code:
$cat file2
111
222

333
444

with line blank,

I need make merge beetwen col so,
Code:
$paste file1 file2 > file3

and exactly
Code:
$cat file3
aaa    111    
bbb    222    
ccc
ddd   
eee 
fff 
     
ggg    333   
hhh    444   
iii 
jjj

Thanks you,


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

Last edited by vbe; 01-12-2015 at 05:16 PM.. Reason: code tags
# 2  
Old 01-12-2015
Confused. What exactly are you asking? It looks like you have resolved your own issue.
This User Gave Thanks to blackrageous For This Post:
# 3  
Old 01-12-2015
Try this (an ad hoc quick and dirty solution for exactly your samples, no error checking etc.):
Code:
awk     '/^$/           {do {ST=getline S0 < F1; print S0} while ((ST==1) && S0!=""); next}
         END            {do {ST=getline S0 < F1; if (ST!=1) exit; print S0} while (S0!="")}
                        {getline S0 < F1; print S0, $0}
        ' F1=file1 file2
aaa 111
bbb 222
ccc
ddd
eee
fff

ggg 333
hhh 444
iii
jjj

This User Gave Thanks to RudiC For This Post:
# 4  
Old 01-12-2015
try also:
Code:
awk '
FNR==1 {l=0}
! /./  {l=1; if (FNR!=NR) print; next}
FNR==NR { if (! l) { a[a1++]=$0; } else { b[b1++]=$0; } ; next }
{if (! l) {c=a[a2++]} else {c=b[b2++]}; print $0, c}
' file2 file1

This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 01-12-2015
For something that can take any number of groups separated by blank lines in both files, your could try:
Code:
awk '
function print_rest() {
	while(c1[g2] > c2)
		printf("%s\t\n", d[g2, ++c2])
	if(++g2 <= g1)
		print ""
	c2 = 0
}
BEGIN {	g1 = g2 = 1
}
FNR == NR {
	if(!NF) {
		g1++
		next
	}
	d[g1, ++c1[g1]] = $1
	next
}
!NF {	print_rest()
	next
}
{	printf("%s\t%s\n", d[g2, ++c2], $1)
}
END {	print_rest()
}' file1 file2

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

If you want to keep the spaces that were present in some of your sample file1 and file2 input lines, change both occurrences of $1 in the above script with $0. (Using $1 seemed to produce output closer to what you said you wanted.)

When run with your sample input files, the above produces the output:
Code:
aaa	111
bbb	222
ccc	
ddd	
eee	
fff	

ggg	333
hhh	444
iii	
jjj

and if you reverse the order of the input files, it produces:
Code:
111	aaa
222	bbb
	ccc
	ddd
	eee
	fff

333	ggg
444	hhh
	iii
	jjj

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 01-13-2015
Very fine

Thanks you

---------- Post updated at 07:20 AM ---------- Previous update was at 05:49 AM ----------

Thaks so much Don Cragun,

Just like this, excelent, one finally question, how I can generality you code for many colums so,

Code:
$cat file1
aaa     
bbb     
ccc
ddd    
eee 
fff
     
ggg    
hhh    
iii
jjj



Code:
$cat file2
111  555  888
222  666  999

333  777 1010
444  777 1111


output

Code:
$cat file3

aaa 111 555 888    
bbb 222 666 999   
ccc 
ddd    
eee 
fff
     
ggg  333 777 1010 
hhh  444 777 1111 
iii
jjj

---------- Post updated at 07:22 AM ---------- Previous update was at 07:20 AM ----------

Thanks you,

Last edited by Don Cragun; 01-13-2015 at 11:00 PM.. Reason: Add CODE tags.
# 7  
Old 01-13-2015
Another way to do the same...
Code:
akshay@Aix:/tmp$ cat f1
aaa     
bbb     
ccc
ddd    
eee 
fff
     
ggg    
hhh    
iii
jjj

Code:
akshay@Aix:/tmp$ cat f2
111
222

333
444

Code:
akshay@Aix:/tmp$ cat test.sh
#!/bin/bash 

awk '
	FNR==NR{ 
		k    += !NF
		s[k]  = k in s ? s[k] SUBSEP $0 : $0 
		next
	}
	FNR==1{
		k = 0
	}
	{ 
		if(FNR==1 || !NF){
			if(z<n)
			{
				while(z != n){  printf("%s%s%s\n", "", OFS, t[z++]) }
			}

			n = split(s[k++],t,SUBSEP)	
			z = !n
		}
		z++
		printf("%s%s%s\n", $0, OFS, ( (NF && z in t) ?  t[z] : "" ) )
        }
    ' OFS='\t'  $1 $2

Code:
akshay@Aix:/tmp$ bash test.sh f1 f2
111	aaa     
222	bbb     
	ccc
	ddd    
	eee 
	fff
	
333	ggg    
444	hhh

Code:
akshay@Aix:/tmp$ bash test.sh f2 f1
aaa     111
bbb     222
ccc	
ddd    	
eee 	
fff	
     	
ggg    	333
hhh    	444
iii	
jjj

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut specific column from 2 file and merge

Hi ALL, I have two file. I need to combine these two file based on a layout. I used the below code and able to extract the record. But now able to insert that to a 3'rd file in between the extract FILE 1 CAID NUMBER 1-20 TID NUMBER 21-22 LABEL CHAR 23-44 BASE 45-60... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

2. Shell Programming and Scripting

Seperated by columns, merge in a file, sort them on common column

Hi All, I have 4 files in below format. I took them as an example. File 1: Cut from position 1-4 then 6-7 then 8-14 then rest left and make them as columns in one new file. Inserting character H to the initial of all line like HCTOT. CTOT 456787897 Low fever CTOR 556712345 High fever... (2 Replies)
Discussion started by: Mannu2525
2 Replies

3. Shell Programming and Scripting

Multiple file merge by column

Hello all, I am quite new in linux shell scripting and I have this issue. I ve got some files including measurements taken every 10minutes for a whole day. File name format is: 00.00, 00.10, 00.20,....23.50 File structure is: x | y | temperature x and y is the same in all files (same... (12 Replies)
Discussion started by: atzounis
12 Replies

4. Shell Programming and Scripting

Merge with common column

hi i have two files and i wanted to join them using common column. try to do this using "join" command but that did not help. File 1: 123 9a.vcf hy92.vcf hy90.vcf Index Ref Alt Ref Alt Ref Alt 315 14 0 7 4 ... (6 Replies)
Discussion started by: empyrean
6 Replies

5. Shell Programming and Scripting

column merge same value

Dear All, I have the following file: file1 "1" 189218400 189221399 3000 "0041 ENSMUSG00000000031" "0041" "+" "ENSMUSG00000000031" 189039418 189175306 "downstream" 178982 43094 "NearestStart" "1" 197067200 197068353 1154 "0057... (3 Replies)
Discussion started by: paolo.kunder
3 Replies

6. Shell Programming and Scripting

Merge CSV files and create a column with the filename from the original file

Hello everyone!! I am not completely new to shell script but I havent been able to find the answer to my problem and I'm sure there are some smart brains here up for the challenge :D. I have several CSV files that I need to combine into one, but I also need to know where each row came from.... (7 Replies)
Discussion started by: fransanchezoria
7 Replies

7. Shell Programming and Scripting

Help with merge two file based on similar column content

Input file 1: A1BG A1BG A1BG A1CF A1CF BCAS BCAS A2LD1 A2M A2M HAT . . Input file 2: A1BG All A1CF TEMP (5 Replies)
Discussion started by: perl_beginner
5 Replies

8. Shell Programming and Scripting

merge two two txt files into one file based on one column

Hi, I have file1.txt and file2.txt and would like to create file3.txt based on one column in UNIX Eg: file1.txt 17328756,0000786623.pdf,0000786623 20115537,0000793892.pdf,0000793892 file2.txt 12521_74_4.zip,0000786623.pdf 12521_15_5.zip,0000793892.pdf Desired Output ... (5 Replies)
Discussion started by: techmoris
5 Replies

9. Shell Programming and Scripting

Merge Two Files based on First column

Hi, I need to join two files based on first column of both files.If first column of first file matches with the first column of second file, then the lines should be merged together and go for next line to check. It is something like: File one: 110001 abc efd 110002 fgh dfg 110003 ... (10 Replies)
Discussion started by: apjneeraj
10 Replies

10. Shell Programming and Scripting

Column Merge?

Hi all, I need to join two columns from two different files file1.txt and file2.txt and append in a new file. Example : $cat file1.txt ABCD.ksh:010141 ABCD.ksh:010511 ABCD.ksh:010815 ABCD.ksh:011114 ABCD.ksh:011415 ABCD.ksh:011720 ABCD.ksh:012022 ABCD.ksh:012830 ABCD.ksh:014432... (5 Replies)
Discussion started by: sabyasm
5 Replies
Login or Register to Ask a Question