grep sed awk实现批量文件改名

时间:2022-02-18 16:16:30

原创: 牛超
2009-05
OSAKA

 

路径下有N个文件,文件名的格式是:filename_YYYYMMDD_序列号(从001到999).dat, 例如:filename_20040108_089.dat

#! /bin/sh
dtnow=`date '+%Y%m%d'`
pattern="*.*"
paths="./"
if [ $# -gt 1 ] ;then
 pattern=$2
elif [ $# -gt 0 ] ; then
 path=$1;
fi
#echo $paths | sed -e "s/.*[^//]$/&///"
paths=`echo $paths | sed "s/.*[^//]$/&///"`
echo "# Path   :$path"
echo "# Pattern:$pattern"
#ls $1 |
find $paths -maxdepth 1 -name "$pattern" |
 sed -e "s//(.*test_/)/([[:digit:]]/{8/}/)_/([[:digit:]]/{3/}/).dat$/& /1${dtnow}_/&/3/&.dat/" |
  grep ".* .*" |
   awk 'BEGIN {FS="&"} {num=($2+500)%1000; printf("%s%03d%s/n" ,$1,num,$3)}' |
    xargs -l mv
echo "$$ Exit"
ls $paths