在 bash 中将来自不同文件的列压缩在一起

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1036008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:00:43  来源:igfitidea点击:

zip columns from separate files together in bash

bash

提问by nedned

I have two files which both contain a list of words. Is there an easy way to zip the contents of the files into one new file in bash, so that the resultant file would have two columns like this:

我有两个文件,它们都包含一个单词列表。有没有一种简单的方法可以将文件的内容压缩到 bash 中的一个新文件中,以便生成的文件有两列,如下所示:

    file1_line1 file2_line1
    file1_line2 file2_line2
    file1_line3 file2_line3
    file1_line4 file2_line4

回答by John Kugelman

NAME

姓名

paste-- merge corresponding or subsequent lines of files

paste-- 合并相应的或后续的文件行

SYNOPSIS

概要

paste [-s] [-d list] file ...

paste [-s] [-d list] file ...

DESCRIPTION

描述

The pasteutility concatenates the corresponding lines of the given input files, replacing all but the last file's newline characters with a single tab character, and writes the resulting lines to standard output.

paste实用程序连接给定输入文件的相应行,用单个制表符替换除最后一个文件的换行符之外的所有行,并将结果行写入标准输出。

回答by Charles Ma

Paste will get you half way there, but you'll need sed to append the file name to the words

Paste 会让你成功,但你需要 sed 将文件名附加到单词中

Put this into a shell script and pass it the two files as arguments

将其放入 shell 脚本并将两个文件作为参数传递给它

#!/bin/sh
paste   | sed -e "s/^\([^ ]\+\)\s\+\([^ ]\)/_ _/"