The original file name is as follows:
21001_20170101_340000_1_HK_BCF_201701122132_2060.txt
21001_20170101_340000_1_LY_BCF_201701122132_2059.txt
210 02_20170101_340000_1_NBCLCK_BCF_201701122132_2064.txt
21002_20170101_340000_1_NBCLJK_BCF_201701122132_2063.txt
2 1003_20170101_340000_1_TD_BCF_201701122132_2061.txt
21002_20170101_340000_1_ZY_BCF_201701122132_2062.txt
shell script is as follows, where tshort=${tfile%BCF*.txt} bcf. txt deletes all the words after BCF and changes them to bcf.txt. tshort=${tshort# _ } further truncates the preceding 210001_(deletes the smallest part that matches *_* and returns the rest).
for tfile in *.txt
do
tshort=${tfile%BCF*.txt}BCF.txt
tshort=${tshort#*_*}
if [ ${tfile} != ${tshort} ]; then
mv ${tfile} ${tshort}
fi
done
The command output is as follows:
20170101_340000_1_HK_BCF.txt
20170101_340000_1_LY_BCF.txt
20170101_340000_1_NBCLCK_BCF.txt
20170101_34000 0_1_NBCLJK_BCF.txt
20170101_340000_1_TD_BCF.txt
20170101_340000_1_ZY_BCF.txt
To keep file names consistent, remove NBCL from some file names. You can do this by using the rename command(change _NBCL to _), i.e.
rename _NBCL _ *BCF.txt
The
rename command is dedicated to renaming files. As you can see from the above example, rename can rename either a single file or a batch file.
Note: The rename command takes three arguments instead of two. The second argument above is left blank to remove the character specified in the first argument.
The command output is as follows:
20170101_340000_1_HK_BCF.txt
20170101_340000_1_LY_BCF.txt
20170101_340000_1_CK_BCF.txt
20170101_340000_1_ JK_BCF.txt
20170101_340000_1_TD_BCF.txt
20170101_340000_1_ZY_BCF.txt
========================================
is a good method, pay attention to the use of wildcards, in fact, the key lies in the use of parameter substitution, the following summary:
${param:-default} If param is empty, the value of the entire expression is default
${#param} The value of the entire expression is the length of param
${param%word} Delete the smallest part matching word starting at the end of param and return the rest
${param%%word} Delete the longest part matching word starting at the end of param and return the rest
${param#word} Delete the smallest part matching word from the param header and return the rest
${param##word} Delete the longest part matching word from the param header and return the rest
The smallest part is the first match, and the longest part is the last match, for example:
Suppose aa=iamlaosongsongdc
then echo ${aa%song*} displays: iamlaosong
while echo ${aa%%song*} displays: iamlao
If you do not use wildcards, echo ${aa%%song} shows iamlaosongsongdc because there is no match.
Add a few more uses
${param:=bar} Checks whether param is empty, and returns its value if it is not; Otherwise assign param to bar and return the value
${param:? If param is empty or absent, output param:bar and terminate the script with an exception
${param:+bar} param is present and not empty, return param only if it is not empty, otherwise return empty(seemingly bar is not used here)
${param:m:n} from the MTH character left of param(0 for the first), take n characters backward, if n is omitted, take all subsequent characters
${param:0-m:n} from the MTH character right of param(0-m equals a negative value), Take n characters backward. If n is omitted, take all subsequent characters
${param:m} starting from the MTH character left of param(0 being the first), and take all characters backward