merge two files horizontally


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merge two files horizontally
# 1  
Old 07-03-2008
merge two files horizontally

Hi all,
I have two files -- one with 1024 lines and another with 2048 lines. I want to merge them to create another file in such way that it takes 2 lines from file with 1024 lines and 4 from 2048 lines.

I wrote a sample script and curious to see if there is a better way; i am sure there is, what will so many genious ppl


Code:
#!/bin/ksh

i=1
j=1
while [[ $i -lt 1025 || $j -lt 2049 ]]
do
    awk "NR==$i,NR==$i+1" wa1009wd00FF.dat >> final_writeFFFFh.txt
    awk "NR==$j,NR==$j+3" writeFFFFh_to_dpram.txt >> final_writeFFFFh.txt
    let i=i+2
    let j=j+4
done

# 2  
Old 07-03-2008
How about something like this (typed, not tested):

Code:
awk '
    {
        print ; getline ; print 
        for (i=1; i<=4 && getline < "writeFFFFh_to_dpram.txt"; i++) { print }
    }
' wa1009wd00FF.dat > final_writeFFFFh.txt

# 3  
Old 07-03-2008
Code:
la=`cat a | wc -l`
lb=`cat b | wc -l`
nawk -v a="$la" -v b="$lb" 'BEGIN{n=1}
{
if (NR==FNR)
{
	if(NR%2!=0)
	{
		t=NR/2-0.5
	}
	tt=NR+t*3
	arr[tt]=$0
}
else
{
	t=FNR+n*2
	arr[t]=$0
	if(FNR%3==0)
		n=n+1
}
}
END{
	sum=a+b
	for(i=1;i<=sum;i++)
		print arr[i]
}' a b

# 4  
Old 07-03-2008
Code:
cat file2 | paste file1 - - | paste - - | awk '{print $1,$4,$2,$3,$5,$6}' | tr " " "\n"

Should work Smilie
# 5  
Old 07-03-2008
cool solutions!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging data horizontally with newlines in files

Hi Everyone, I have two files file1 and file2 with these contents cat file1 AAAAA 01/03/2014 04:01:23 BBBB 01/03/2014 03:03:34 CCCcc 01/03/2014 03:03:34 cat file2 1 RED 1 HHHH 1 TTTT 1 BBBBB I tried the below... (2 Replies)
Discussion started by: Aditya_001
2 Replies

2. Shell Programming and Scripting

Merge files and generate a resume in two files

Dear Gents, Please I need your help... I need small script :) to do the following. I have a thousand of files in a folder produced daily. I need first to merge all files called. txt (0009.txt, 0010.txt, 0011.txt) and and to output a resume of all information on 2 separate files in csv... (14 Replies)
Discussion started by: jiam912
14 Replies

3. Shell Programming and Scripting

How to merge two files?

Hi Gurus, I have two files as below file1 abc cde cdd cdf file2 123 234 345 456 I want to get abc 123 cde 234 cdd 345 (3 Replies)
Discussion started by: ken6503
3 Replies

4. Shell Programming and Scripting

Merge files

a.txt id name subject 12 aaa History 23 bbb Science 45 ccc Zoology b.txt id layer LayerNo 12 xxx12 1 23 yyy23 2 23 lll23 3 45 xxx45 1 45 yyy45 2 45 lll45 3 i have file a.txt which is parent file and another children file b.txt . Both files are linked together by common field... (2 Replies)
Discussion started by: manas_ranjan
2 Replies

5. Shell Programming and Scripting

Checking in a directory how many files are present and basing on that merge all the files

Hi, My requirement is,there is a directory location like: :camp/current/ In this location there can be different flat files that are generated in a single day with same header and the data will be different, differentiated by timestamp, so i need to verify how many files are generated... (10 Replies)
Discussion started by: srikanth_sagi
10 Replies

6. Shell Programming and Scripting

merge files

hi, i've two files whcih i need to merge in below format. File1- DEFINE lc_driver CHAR(3) INITIALIZE gr_XXX_MAIN_TABLE_XXX.* TO NULL { EXECUTE p_XXX_MAIN_TABLE_XXX_ins USING gr_XXX_MAIN_TABLE_XXX.* } File2 - # field1 LET table1.field1= (8 Replies)
Discussion started by: dvah
8 Replies

7. Shell Programming and Scripting

writing the content of two files to a single file horizontally

Hi all, assume that i am having two files file.1 and file.2 these are the contents of file.1 these are the contents of file.2 i want these two contents to be written in another single file like this. can you please post your suggestions. Thanks in advance, ... (3 Replies)
Discussion started by: anishkumarv
3 Replies

8. Shell Programming and Scripting

Merge 2 files

Hi , This might be the stupidest question I am asking, but I am struck with this problem. I am trying to merge 2 files, file1 has header and file2 has contents. while I merge them , it merges from the 1st line of file1. for ex: file1 col1|col2|col3| file2 123|234|456|... (2 Replies)
Discussion started by: rashmisb
2 Replies

9. Shell Programming and Scripting

Merge files of differrent size with one field common in both files using awk

hi, i am facing a problem in merging two files using awk, the problem is as stated below, file1: A|B|C|D|E|F|G|H|I|1 M|N|O|P|Q|R|S|T|U|2 AA|BB|CC|DD|EE|FF|GG|HH|II|1 .... .... .... file2 : 1|Mn|op|qr (2 Replies)
Discussion started by: shashi1982
2 Replies

10. HP-UX

Combining files horizontally

I have 2 files, 1.txt and 2.txt which have the following format: 1.txt :451:1 :451:0 :451:1 and so on.. 2.txt :108:13897187 :108:90890789 :108:76476386 and so on.. I want to combine the files horizontally, I tried paste 1.txt 2.txt as well as cat but both of them are giving me... (3 Replies)
Discussion started by: anshuljain
3 Replies
Login or Register to Ask a Question