相关疑难解决方法(0)

使用 zipper 方法合并文件/后期合并

我正在寻找一种使用 zipper 方法(也称为后期合并)逐行合并两个或多个文件的好方法。假设我们有三个文件,结果应该是这样的:

line1 file1
line1 file2
line1 file3
line2 file1
line2 file2
line2 file3
...
Run Code Online (Sandbox Code Playgroud)

编辑

我写了一个能够做到这一点的小python脚本:

#!/usr/bin/python

import sys, itertools

fileList = []
for file in sys.argv[1:]:
    f = open(file, "r")
    fileList.append(f.read().split("\n"))

for z in itertools.izip_longest(*fileList):
    print "\n".join([i for i in z if i is not None])
Run Code Online (Sandbox Code Playgroud)

我仍然想知道是否有任何标准工具或它们的巧妙组合来做同样的事情。

text-processing merge

7
推荐指数
1
解决办法
594
查看次数

如何将两个文件合并为一个,每个文件的数据都有列?

我有两个文件:

文件 1 包含:

1
2
3
4
Run Code Online (Sandbox Code Playgroud)

文件 2 包含:

John
Sam
George
Ken
Run Code Online (Sandbox Code Playgroud)

我想合并这些文件来创建一个文件(file3)

1, John
2, Sam
3, George
4, Ken
Run Code Online (Sandbox Code Playgroud)

我的想法是使用嵌套循环并为每一行添加逗号,

for x in file1
do
echo "$x" >> file3
for y in file2
echo ",$y" >> file3
done
done
Run Code Online (Sandbox Code Playgroud)

有我需要使用的命令吗?如何让 x 和 y 出现在两个文件中的每个条目的一行上?

shell-script

6
推荐指数
1
解决办法
2万
查看次数

在每行的末尾添加一个后缀字符串?

我有两个文件:

文件01:

line1
line2
line3
Run Code Online (Sandbox Code Playgroud)

文件02:

A
B
C
Run Code Online (Sandbox Code Playgroud)

我想得到

line1 A
line2 B
line3 C
Run Code Online (Sandbox Code Playgroud)

我可能会写一个 Perl 脚本,但如果我不需要它会更好。

我试过使用明显的 grep -o '^' file02 >> file01

但这会产生

line1
line2
line3
A
B
C
Run Code Online (Sandbox Code Playgroud)

这应该是一件很简单的事情,但在“man grep”(grep手册)中没有找到。

linux grep scripting bash sed

2
推荐指数
1
解决办法
1605
查看次数

标签 统计

bash ×1

grep ×1

linux ×1

merge ×1

scripting ×1

sed ×1

shell-script ×1

text-processing ×1