如何对多个文件使用 unix/shell 粘贴命令
How to use the unix/shell paste command for several files
我有五个 csv 文件,我想使用 shell 函数将它们 paste
在一起。这基本上执行了几个文本文件中行的连接。我所追求的是在示例 8 in this tutorial
中看到的
我从 Python 通过 subprocess.call()
执行此操作,但是直接在终端中执行此操作会产生相同的令人困惑的结果。
我的文件都是制表符分隔符(这是粘贴功能的默认分隔符)
当我在 2、3、... n 个文件上使用该函数时,似乎第二到第 n 个文件的 header 被添加为第二行,header 仅出现在第一行的第一个文件。
这是我的命令:
paste outfile.txt tmp_1.txt tmp_2.txt tmp_3.txt tmp_4 > final.txt
这是输出:
col1 col2 col3 # <-- 1st file has 3 columns
col4 col5 # <-- 2nd file has 2 columns
col6 # <-- 3rd file has 1 columns
col7 # <-- 4th file has 1 columns
col8 col9 # <-- 5th file has 2 columns
然而,在此之后,行以不同的方式继续(一直到文件末尾):
col1 col2 col3
col4 col5 col6 col6 col7 col8 col9
col1 col2 col3
col4 col5 col6 col6 col7 col8 col9
[这两个代码块相互依存]
我找不到我可以在 this documentation 中指定的更多选项,明确输入 -d'\t'
不会改变任何内容。我也尝试了更少或更多的文件,改变了文件的顺序(以防我的第一个文件中有一些 carriage returns 等,但结果总是一样的。
更新#1
这是 @shellter 在评论中推荐的命令的输出片段:cat -vet file1.txt file2.txt ... file5.txt | less
:
Col1^ICol2^ICol3^M$
Some text was here^I2^I-3^M$
Some text was here^I2^I-1^M$
Some text was here^I2^I-2^M$
Some text was here^I2^I-1^M$
您可以看到选项卡的 ^I
标记和 end-of-line / carriage-return / 换行符的 ^M
加上 $
。
更新 #2
已将 shell 函数 dos2unix
应用于我的文件:
dos2unix file1.txt file2.txt ... file5.txt
我原来使用的原始粘贴功能可以正常使用。从最终文件的输出中,我们可以看到哪些标记仍然是有用的。这是期望的结果,已实现:
col1 col2 col3 col4 col5 col6 col6 col7 col8 col9
col1 col2 col3 col4 col5 col6 col6 col7 col8 col9
col1 col2 col3 col4 col5 col6 col6 col7 col8 col9
此处用于检查的函数的输出:cat -vet file1.txt ...
:
Col1^ICol2^ICol3^ICol4^ICol5^ICol6^Col7^ICol8^ICol9$
Col1^ICol2^ICol3^ICol4^ICol5^ICol6^Col7^ICol8^ICol9$
Col1^ICol2^ICol3^ICol4^ICol5^ICol6^Col7^ICol8^ICol9$
找不到 ^M
个标记。
正在将一些评论转移到(社区 Wiki)答案中。
Have you got any DOS line endings confusing things? That is, do the files have CRLF line endings?
并且shellter评论:
Use cat -vet file ... file | less
and look for ^M
at the end of each line.
您确认这确实是问题的根源。
我有五个 csv 文件,我想使用 shell 函数将它们 paste
在一起。这基本上执行了几个文本文件中行的连接。我所追求的是在示例 8 in this tutorial
我从 Python 通过 subprocess.call()
执行此操作,但是直接在终端中执行此操作会产生相同的令人困惑的结果。
我的文件都是制表符分隔符(这是粘贴功能的默认分隔符)
当我在 2、3、... n 个文件上使用该函数时,似乎第二到第 n 个文件的 header 被添加为第二行,header 仅出现在第一行的第一个文件。
这是我的命令:
paste outfile.txt tmp_1.txt tmp_2.txt tmp_3.txt tmp_4 > final.txt
这是输出:
col1 col2 col3 # <-- 1st file has 3 columns
col4 col5 # <-- 2nd file has 2 columns
col6 # <-- 3rd file has 1 columns
col7 # <-- 4th file has 1 columns
col8 col9 # <-- 5th file has 2 columns
然而,在此之后,行以不同的方式继续(一直到文件末尾):
col1 col2 col3
col4 col5 col6 col6 col7 col8 col9
col1 col2 col3
col4 col5 col6 col6 col7 col8 col9
[这两个代码块相互依存]
我找不到我可以在 this documentation 中指定的更多选项,明确输入 -d'\t'
不会改变任何内容。我也尝试了更少或更多的文件,改变了文件的顺序(以防我的第一个文件中有一些 carriage returns 等,但结果总是一样的。
更新#1
这是 @shellter 在评论中推荐的命令的输出片段:cat -vet file1.txt file2.txt ... file5.txt | less
:
Col1^ICol2^ICol3^M$
Some text was here^I2^I-3^M$
Some text was here^I2^I-1^M$
Some text was here^I2^I-2^M$
Some text was here^I2^I-1^M$
您可以看到选项卡的 ^I
标记和 end-of-line / carriage-return / 换行符的 ^M
加上 $
。
更新 #2
已将 shell 函数 dos2unix
应用于我的文件:
dos2unix file1.txt file2.txt ... file5.txt
我原来使用的原始粘贴功能可以正常使用。从最终文件的输出中,我们可以看到哪些标记仍然是有用的。这是期望的结果,已实现:
col1 col2 col3 col4 col5 col6 col6 col7 col8 col9
col1 col2 col3 col4 col5 col6 col6 col7 col8 col9
col1 col2 col3 col4 col5 col6 col6 col7 col8 col9
此处用于检查的函数的输出:cat -vet file1.txt ...
:
Col1^ICol2^ICol3^ICol4^ICol5^ICol6^Col7^ICol8^ICol9$
Col1^ICol2^ICol3^ICol4^ICol5^ICol6^Col7^ICol8^ICol9$
Col1^ICol2^ICol3^ICol4^ICol5^ICol6^Col7^ICol8^ICol9$
找不到 ^M
个标记。
正在将一些评论转移到(社区 Wiki)答案中。
Have you got any DOS line endings confusing things? That is, do the files have CRLF line endings?
并且shellter评论:
Use
cat -vet file ... file | less
and look for^M
at the end of each line.
您确认这确实是问题的根源。