我有txt文件,所有这些文件都在同一目录中.每个都有2列数据.它们看起来像这样:
Label1 DataA1
Label2 DataA2
Label3 DataA3
我想使用join来创建一个像这样的大文件.
Label1 DataA1 DataB1 DataC1
Label2 DataA2 DataB2 DataC2
Label3 DataA3 DataB3 DataC3
目前,我有
加入fileA fileB | 加入 - fileC
但是,我有太多的文件使列出所有这些文件变得切实可行 - 有没有办法为这种命令编写循环?
该脚本将多个文件连接在一起(文件是file*)。
#!/bin/bash
# Create two temp files
tmp=$(mktemp)
tmp2=$(mktemp)
# for all the files
for file in file*
do
# if the tmp file is not empty
if [ -s "$tmp" ]
then
# then join the tmp file with the current file
join "$tmp" "$file" > "$tmp2"
else
# the first time $tmp is empty, so we just copy the file
cp "$file" "$tmp2"
fi
cp "$tmp2" "$tmp"
done
cat "$tmp"
Run Code Online (Sandbox Code Playgroud)
我承认它很难看,但似乎有效。