如何从目录中的所有文件复制第二列并将它们作为列放在新的文本文件中

How to copy second column from all the files in the directory and place them as columns in a new text file

我有 150 个制表符分隔的文本文件,我想复制每个文件的第 2 列并粘贴到新的文本文件中。新文件将包含每个文件的第 2 列的 150 列。帮帮我伙计们。 此代码有效但将每一列放在另一列之下,形成一个 loooong 列。

for file in *.txt
do
   awk '{print }' *.txt > AllCol.txt
done
#!/bin/bash

# Be sure the file suffix of the new file is not .txt
OUT=AllColumns.tsv
touch $OUT

for file in *.txt
do
  paste $OUT <(awk -F\t '{print }' $file) > $OUT.tmp
  mv $OUT.tmp $OUT
done

许多备选方案之一是使用 cut -f 2 而不是 awk,但您使用 awk 标记了您的问题。

由于您的文件非常规则,您也可以跳过 do 循环,并使用命令行实用程序,例如 rs(整形)或 datamash

这是另一种没有循环的方法

$ c=$(ls -1 file*.tsv | wc -l); cut -f2 file*.tsv | pr  -$c -t