我在目录中有四个文件,如下所示
Test_File_20170101_20170112_1.txt
Test_File_20170101_20170112_2.txt
Test_File_20170101_20170112_3.txt
Test_File_20170101_20170112_4.txt
我想按顺序合并它们,并希望最终文件为
Test_File_20170101_20170112.txt
答案 0 :(得分:1)
您可以执行以下操作:
ls *_[1-9].txt \
| sed 's/_[1-9]\.txt//' \
| sort -u \
| xargs -n 1 -I {} sh -c "cat {}_*.txt > {}.txt"
解释每个步骤:
ls *_[1-9].txt
:列出所有以_1.txt
,_2.txt
等结尾的文件sed 's/_[1-9]\.txt//'
:删除扩展名和数字后缀sort -u
:唯一的文件名(例如Test_File_20170101_20170112
)xargs ...
:对于每个文件名,将每个编号的文件分为一个新文件您可以将其扩展到更大的序列,例如_10.txt
等,但是您需要注意该顺序是不正确的,因为在最后一个*
的扩展中,其顺序是字母顺序,例如_1
,_10
,_2
...这是一些解决方法:cat files in specific order based on number in filename
答案 1 :(得分:0)
我完成以下操作。您的怀疑很好,但我被困在@cmbuckley帮助的1点,我的代码按预期工作。感谢您的关注和帮助。如果有任何不正确的地方,但仍然可以正常工作,您仍然可以更正我的代码。
#!/bin/bash
PDIR=$1
FDIR=$2
# Change the current working directory
cd "$PDIR" || exit;
# Count number of files present in the current working directory
c=$(ls -p | grep -v / | wc -l)
# Count number of iteration it needed for Loop
n=$(expr "$c" / 4)
# Move the first 4 sorted files.
# 1.Header File 2.Column Name File 3.Detail records file 4.Trailer record file
i=1
while [ "$i" -le "$n" ];
# Move first 4 files from source folder to processing folder
do mv `ls -p | grep -v / | head -4` "$PDIR"merge/;
# Change directory to processing
cd "$PDIR"merge/ || exit;
# Look for files ending with "_[1-9], sort and then merge them to one file and remove numeric from output file
ls *_[1-9].txt | sort -u | sed 's/_[1-9]\.txt//' | xargs -n 1 -I {} sh -c "cat {}_*.txt > {}.txt";
# Remove processed files
rm -f *_[1-9].txt;
# Move output file to Target directory
mv *.txt "$FDIR";
cd "$PDIR" || exit;
i=$(($i + 1));
done