简体   繁体   English

使用基本 ubuntu shell 命令将单行文本合并到数据框中

[英]Merge one-line texts into a data-frame with basic ubuntu shell commands

I have let's say two files Input1.txt and Input2.txt .我假设两个文件Input1.txtInput2.txt Each of them is a text file containing a single line of 5 numbers separated by a tab.它们中的每一个都是一个文本文件,其中包含由制表符分隔的 5 个数字的单行。

For instance Input1.txt is例如Input1.txt

1 2 3 4 5

and Input2.txt is Input2.txt

6 7 8 9 10

The output that I desire is Output.txt :我想要的 output 是Output.txt

Input1 1 2 3 4 5
Input2 6 7 8 9 10

So I want to merge the files in a table with an extra first column containing the names of the original files.所以我想将表中的文件与包含原始文件名称的额外第一列合并。 Obviously I have more than 2 files (actually 1000) and I would like to make it with a for loop.显然我有超过 2 个文件(实际上是 1000 个),我想用一个 for 循环来制作它。 You can assume that all my files are named as Input*.txt with * between 1 and 1000 and that they are all in the same directory.您可以假设我的所有文件都命名为 Input*.txt,其中 * 介于 1 和 1000 之间,并且它们都在同一个目录中。

I know how to do it with R, but I would like to make it with a basic line of commands in the ubuntu shell.我知道如何使用 R 来实现,但我想使用 ubuntu shell 中的基本命令行来实现。 Is it feasible?可行吗? Thanks for any help.谢谢你的帮助。

Assuming the line in Input1.txt , Input2.txt , etc. is terminated with a newline character, you can use假设Input1.txtInput2.txt等中的行以换行符终止,您可以使用

for i in Input*.txt
do
    printf "%s " "$i"
    cat "$i"
done > Output.txt

The result is结果是

Input1.txt 1 2 3 4 5
Input2.txt 6 7 8 9 10

If you want to get Input1 etc. without .txt you can use如果你想在没有.txt的情况下获得Input1等,你可以使用

    printf "%s " "${i%.txt}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM