开发者

How can I merge two files?

开发者 https://www.devze.com 2023-01-18 21:52 出处:网络
I have two files File 1: 7118开发者_运维技巧 7457 7591 7539 8001 File 2: 5003 5008 5011 5026 5028 5029 5031 Output that I need

I have two files

File 1:

7118开发者_运维技巧
7457
7591
7539
8001

File 2:

5003
5008
5011
5026
5028
5029
5031

Output that I need

7118,5003
7457,5003
7591,5003
7539,5003
8001,5003
7118,5008
7457,5008
7591,5008
7539,5008
8001,5008

And so on.....


awk 'FNR==NR{a[$0];next}{ for(i in a) print i,$0 }' OFS="," file file1


And a bash function:

function cross() {
    exec 3<$2
    while read -u 3 a ; do
        exec 4<$1
        while read -u 4 b ; do
            echo $b,$a
        done
    done
    3<&-
    4<&-
}


I'll use a perl script for that.

#!/usr/bin/perl

use strict;

my @file1 = loadf("file1.txt");
my @file2 = loadf("file2.txt");

foreach my $line2 (@file2) {
    $line2 =~ s/^\s+//;
    $line2 =~ s/\s+$//;
    for (my $i = 0; $i < @file1; $i++) {
        $file1[$i] =~ s/^\s+//;
        $file1[$i] =~ s/\s+$//;
        #do the output
        print $file1[$i] . "," . $line2 . "\n";
    }

}

sub loadf($) {
    my @file = ( );
    open(FILE, $_[0] . "\n") or die("[-] Couldn't Open " . $_[0] . "\n");
    @file = <FILE>;
    close(FILE);
    return @file;
}


cat file1 | (exec 3< file2; while read A && read B <&3; do echo "$A,$B"; done)
0

精彩评论

暂无评论...
验证码 换一张
取 消