Special Merge of two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Special Merge of two files
# 1  
Old 08-18-2011
Power Special Merge of two files

I have two files, foo1 and foo2. I would like to merge them into file named foo3 such that field 1,2,3,4,5,ect of foo1 are fields 1,3,5,7,9,ect of foo3, and fields field 1,2,3,4,5,ect of foo2 are fields 2,4,6,8,10,ect of foo3. Foo1 and Foo2 are the same size of 800 fields and 20K lines.


eg foo1:
Code:
1 1 1 1 0...
0 1 0 1 0...
.
.
.

and foo2:
Code:
0 0 0 0 0...
0 0 0 1 1...
.
.
.

and desired merged file, foo3:
Code:
1 0 1 0 1 0 1 0 0 0 ...
0 0 1 0 0 0 1 1 0 1 ...
.
.
.

SmilieThanks in advance!
# 2  
Old 08-18-2011
Hi,

Test next 'perl' script:
Code:
$ cat file1
1 2 3 4 5
6 7 8 9 10
$ cat file2
20 21 22 23 24 
25 26 27 28 29
$ cat script.pl
use warnings;
use strict;
use autodie;

@ARGV == 2 or die qq(Usage: perl $0 file1 file2\n);

open my $fh_odd, "<", $ARGV[0];
open my $fh_even, "<", $ARGV[1];

while ( my $line_odd = <$fh_odd> ) {
        my @line_combined = ();
        my $line_even = <$fh_even>;
        my @line_odd = split /\s+/, $line_odd;
        my @line_even = split /\s+/, $line_even;
        push @line_combined, shift @line_odd, shift @line_even while @line_odd;
        printf "%s\n", "@line_combined";
}

$ perl script.pl file1 file2
1 20 2 21 3 22 4 23 5 24
6 25 7 26 8 27 9 28 10 29

Regards,
Birei
# 3  
Old 08-18-2011
Thanks for the response,

I ran the code using your test files and got the following message:


Code:
Can't locate autodie.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at script.perl line 3.

Does this mean my perl version is not up to date?
# 4  
Old 08-18-2011
Yes, it's not up to date, but there's not much problem, change the script a little bit:
Code:
$ cat script.pl
use warnings;
use strict;
#use autodie;

@ARGV == 2 or die qq(Usage: perl $0 file1 file2\n);

open my $fh_odd, "<", $ARGV[0] or die qq(Cannot open input file $ARGV[0]: $!\n);
open my $fh_even, "<", $ARGV[1] or die qq(Cannot open input file $ARGV[1]: $!\n);

while ( my $line_odd = <$fh_odd> ) {
        my @line_combined = ();
        my $line_even = <$fh_even>;
        my @line_odd = split /\s+/, $line_odd;
        my @line_even = split /\s+/, $line_even;
        push @line_combined, shift @line_odd, shift @line_even while @line_odd;
        printf "%s\n", "@line_combined";
}

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 5  
Old 08-18-2011
If you want to use basic unix shell commands:
Code:
#!/bin/ksh
typeset -i mI1
paste -d' ' File1 File2 | while read mLine; do
  set -A mArr1 $(echo ${mLine} | cut -d' ' -f1-5)
  set -A mArr2 $(echo ${mLine} | cut -d' ' -f6-)
  mOutLine=''
  mI=0
  while [[ ${mI} -le 4 ]]; do
    mOutLine=${mOutLine}' '${mArr1[$mI]}' '${mArr2[$mI]}
    mI=${mI}+1
  done
  echo "mOutLine <${mOutLine}>"
done

This User Gave Thanks to Shell_Life For This Post:
# 6  
Old 08-18-2011
Awesome! Thanks!
# 7  
Old 08-18-2011
Code:
paste foo1 foo2 | awk '{for(i=1; i<=NF/2; i++) printf("%s%s%s%s", $i, OFS, $(i+NF/2), i==NF/2 ? ORS : OFS)}' > foo3

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge 3 files

help with a command please i have 3 files that i need to merge in to a 4th file using awk or sed the files have more than 3 lines in them file 1 AAA BBB CCCfile 2 AAA BBB CCCfile 3 AAA BBB CCCi want the 4th file to look like this after merging AAA AAA AAA (3 Replies)
Discussion started by: bob123
3 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

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

5. UNIX for Dummies Questions & Answers

Merge files

Hi, I would like to know how can I merge files based on their coordinates, but mantaining the score of each file in the output file like: Note: 1st column is for chromosome, 2nd for start, 3rd for end of segment, 4th for score file1: 1 200 300 20 1 400 500 30 file2: 1 200 350 30 1... (1 Reply)
Discussion started by: fadista
1 Replies

6. Shell Programming and Scripting

merge two different files

HI I have input file as date 22jan2011 calc 0 667788.1 998877.2 vals 1 222 444 666 777 999 vals 2 222 444 666 777 999 vals 3 ... (3 Replies)
Discussion started by: Indra2011
3 Replies

7. Shell Programming and Scripting

Merge files

Hello, I have a application software plink. It can merge files with some kinds of way. The command likes: plink --file 1 --merge 2 --recode --out merged That means merge file 1 and 2 then output file "merged". However I have 23 files (1,2,3,...22,23)to be merged together. How can I use... (2 Replies)
Discussion started by: zhshqzyc
2 Replies

8. Shell Programming and Scripting

How can i merge these two files into several...

Given are File A and File B File A has for example 5 lines: AAA BBB CCC DDD EEE File B has 3 lines: 111 222 333 How can i merge A and B into: 111 222 333 AAA (first line from A) then a new file: (4 Replies)
Discussion started by: Y-T
4 Replies

9. Shell Programming and Scripting

Merge 2 files

Hello, i'd like a bash script to merge 2 files without duplicate lines. Example : file1 : toto titi file2 : toto tata Expected result, file3 : toto (5 Replies)
Discussion started by: Celmar
5 Replies

10. 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
Login or Register to Ask a Question