从文件名中删除某些字符
Remove certain characters from file name
我在一个目录中有四个文件,假设如下
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
你可以这样做:
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
我完成以下内容。你的怀疑很好,但我被困在 @cmbuckley 帮助的 1 点,我的代码按我的预期工作。感谢您的关心和帮助。如果有任何不对但它工作正常,您仍然可以更正我的代码。
#!/bin/bash
PDIR=
FDIR=
# 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
我在一个目录中有四个文件,假设如下
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
你可以这样做:
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
我完成以下内容。你的怀疑很好,但我被困在 @cmbuckley 帮助的 1 点,我的代码按我的预期工作。感谢您的关心和帮助。如果有任何不对但它工作正常,您仍然可以更正我的代码。
#!/bin/bash
PDIR=
FDIR=
# 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