如何合并文件阿?

sh/bash/dash/ksh/zsh等Shell脚本
nangergong
帖子: 103
注册时间: 2008-09-19 3:26

如何合并文件阿?

#1

帖子 nangergong » 2009-06-19 23:11

比如一个文件是
1
2
3
另一个文件是
a
b
c
現在要合并为的文件是
1 a
2 b
3 c
头像
ptpt52
帖子: 717
注册时间: 2008-07-27 8:51
系统: Ubuntu/Windows
来自: 广西玉林|广东深圳
联系:

Re: 如何合并文件阿?

#2

帖子 ptpt52 » 2009-06-19 23:22

自己写 个 程序 吧
头像
peachcolor
帖子: 898
注册时间: 2006-05-20 14:03

Re: 如何合并文件阿?

#3

帖子 peachcolor » 2009-06-19 23:25

用python写个脚本倒是很简单,用sh的话不知道了。
nangergong
帖子: 103
注册时间: 2008-09-19 3:26

Re: 如何合并文件阿?

#4

帖子 nangergong » 2009-06-19 23:30

那就用python也行阿
怎么弄阿
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

Re: 如何合并文件阿?

#5

帖子 oneleaf » 2009-06-19 23:50

代码: 全选

a=open("a.txt")
b=open("b.txt")
c=open("c.txt","w")
for a1 in a.readlines():
    for b1 in b.readlines():
         c.write(a1.strip()+' '+b1)
c.close()
头像
ptpt52
帖子: 717
注册时间: 2008-07-27 8:51
系统: Ubuntu/Windows
来自: 广西玉林|广东深圳
联系:

Re: 如何合并文件阿?

#6

帖子 ptpt52 » 2009-06-19 23:59

代码: 全选

a=open("a.txt")
b=open("b.txt")
c=open("c.txt","w")
for a1 in a.readlines():
         c.write(a1+' '+b.readlines())
c.close()
应该这样吧 :em09
nangergong
帖子: 103
注册时间: 2008-09-19 3:26

Re: 如何合并文件阿?

#7

帖子 nangergong » 2009-06-20 0:04

非常感谢
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

Re: 如何合并文件阿?

#8

帖子 oneleaf » 2009-06-20 0:10

代码: 全选

a=open("a.txt")
b=open("b.txt")
c=open("c.txt","w")
for a1 in a.readlines():
     c.write(a1.strip()+' '+b.readline())
c.close()
太困,迷糊了。
cheeselee
帖子: 353
注册时间: 2008-07-09 0:39

Re: 如何合并文件阿?

#9

帖子 cheeselee » 2009-06-20 0:59

来段Perl,虽然不太严谨:

代码: 全选

#!/usr/bin/perl
open A,"<a.txt";
open B,"<b.txt";
open C,">c.txt";
while (chomp($a=<A>) and $b=<B>){
    print C "$a $b";
}
7号机-----神舟 优雅A460P-i7G D2
CPU: i7-2670QM
RAM: 8G
HD:500G
OS:Fedora 17 x86_64

热衷话题:Package Management\Programming
头像
xhy
帖子: 3916
注册时间: 2005-12-28 1:16
系统: Ubuntu 12.10 X64
来自: 火星

Re: 如何合并文件阿?

#10

帖子 xhy » 2009-06-20 13:21

代码: 全选

a = open("1")
b = open("2")
c = open("3", "w")
for l in zip(a,b):
    c.write("%s %s" %(l[0][:-1], l[1]) )
目前负债150多万
头像
xhy
帖子: 3916
注册时间: 2005-12-28 1:16
系统: Ubuntu 12.10 X64
来自: 火星

Re: 如何合并文件阿?

#11

帖子 xhy » 2009-06-20 13:24

代码: 全选

a = open("1")
b = open("2")
c = open("3", "w")
map(lambda l:c.write("%s %s" %(l[0][:-1], l[1])), zip(a,b) )
目前负债150多万
sinic
帖子: 13
注册时间: 2008-02-10 20:18

Re: 如何合并文件阿?

#12

帖子 sinic » 2009-06-21 16:55

nangergong 写了:比如一个文件是
1
2
3
另一个文件是
a
b
c
現在要合并为的文件是
1 a
2 b
3 c

paste file1 file2 >file3
详细的man paste
chpn
帖子: 415
注册时间: 2007-02-28 11:33

Re: 如何合并文件阿?

#13

帖子 chpn » 2009-06-21 17:26

佩服ls
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: 如何合并文件阿?

#14

帖子 tusooa » 2009-06-25 15:12

代码: 全选

#!/usr/bin/env python
import sys
fa = file(sys.argv[1],'r')
fb = file(sys.argv[2],'r')
fc = file(sys.argv[3],'w')
c = ""
while True:
    la = fa.readline()
    lb = fb.readline()
    c = c + la + " " + lb
    if len(la) == 0 or len(lb) == 0:
        break

fa.close()
fb.close()
fc.write(c)
fc.close()

代码: 全选

] ls -ld //
头像
kwarph
帖子: 51
注册时间: 2006-06-19 19:19
联系:

Re: 如何合并文件阿?

#15

帖子 kwarph » 2009-07-02 17:58

凑个热闹,C++的(比较笨重)

代码: 全选

#include <fstream>
#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    if (argc < 4)
        cout << "Usage: " << argv[0] << " inputfile1 inputfile2 outputfile"
                << endl;

    ifstream fin1(argv[1]);
    ifstream fin2(argv[2]);

    if (fin1.fail()) {
        cerr << "open file " << argv[1] << " failed." << endl;
        return 1;
    }

    if (fin2.fail()) {
        cerr << "open file " << argv[2] << " failed." << endl;
        return 1;
    }

    ofstream fout(argv[3]);

    string line1, line2;
    while (getline(fin1, line1)) {
        getline(fin2, line2);
        fout << line1 << ' ' << line2 << endl;
    }

    fout.close();
    fin2.close();
    fin1.close();

    return 0;
}
Tiger Cheng
昆山轩辕IT培训中心
C++培训讲师
邮件:kwarph@gmail.com
主页:http://www.xuanyuan-soft.cn
回复