Bash 用前缀连接文件
假设文件夹中的文件名如下:Bash 用前缀连接文件,bash,shell,Bash,Shell,假设文件夹中的文件名如下: FOO.1 FOO.2 ... BAR-1.1 BAR-1.2 ... BAR-2.1 BAR-2.2 ... 我想将它们连接在一起,从而生成3个文件: FOO (consisting of FOO.1 + FOO.2 + FOO.N) BAR-1 (consisting of BAR-1.1 + BAR-1.2 + BAR-1.N) BAR-2 (consisting of BAR-2.1 + BAR-2.2 + BAR-2.N) 在bash/shell脚本中如
FOO.1
FOO.2
...
BAR-1.1
BAR-1.2
...
BAR-2.1
BAR-2.2
...
我想将它们连接在一起,从而生成3个文件:
FOO (consisting of FOO.1 + FOO.2 + FOO.N)
BAR-1 (consisting of BAR-1.1 + BAR-1.2 + BAR-1.N)
BAR-2 (consisting of BAR-2.1 + BAR-2.2 + BAR-2.N)
在bash/shell脚本中如何实现这一点?假设所有文件都在一个文件夹中(无需进入子文件夹)
需要事先不知道文件名前缀需要事先不知道文件名前缀-上面进行了编辑以反映这一点
for i in $(ls | sed 's/\(.*\)\..$/\1/' | sort -u)
do
cat $i* > $i
done
for file in *.*
do
prefix="${file%.*}"
echo "Adding $file to $prefix ..."
cat "$file" >> "$prefix"
done