Linux 使用sed或awk重命名多个文件
我有一台DVR录像机,它用一个特定的名称将文件发送到我的linux ftp服务器,但我需要更改为另一种名称格式,以便可以在旧软件上使用它们。 原始文件采用以下格式:Linux 使用sed或awk重命名多个文件,linux,bash,awk,sed,Linux,Bash,Awk,Sed,我有一台DVR录像机,它用一个特定的名称将文件发送到我的linux ftp服务器,但我需要更改为另一种名称格式,以便可以在旧软件上使用它们。 原始文件采用以下格式: DVR_ch1_principal_20150427094000_20150427100000.dav DVR=Device Name ch1=channel# principal=stream type 20150427094000=starting date in year,month,day,hour,minute,se
DVR_ch1_principal_20150427094000_20150427100000.dav
DVR=Device Name
ch1=channel#
principal=stream type
20150427094000=starting date in year,month,day,hour,minute,second format
20150427100000=end date in year,month,day,hour,minute,second format
ch1_09.04.00-10.00.00.dav
ch1=channel#
09.04.00=starting time in hh.mm.ss
10.00.00=end time in hh.mm.ss
我需要将文件夹中的所有文件修改为以下格式:
DVR_ch1_principal_20150427094000_20150427100000.dav
DVR=Device Name
ch1=channel#
principal=stream type
20150427094000=starting date in year,month,day,hour,minute,second format
20150427100000=end date in year,month,day,hour,minute,second format
ch1_09.04.00-10.00.00.dav
ch1=channel#
09.04.00=starting time in hh.mm.ss
10.00.00=end time in hh.mm.ss
我知道这可以用sed或ark来完成,但这超出了我的知识范围
提前谢谢
sed -n '
# ch1_09.04.00-10.00.00.dav
/^DVR_ch/ {
s/.*\(ch[0-9]\{1,\}\).*\([0-9]\{6\}\)_.*\(([0-9]\{6\}.\{4\}\)$/\1-\2-\3
s/\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/\.\2.\3\
/g
p
}
#ch1=channel
/=channel/ p
#=starting date
/=starting date/ s/[0-9]*\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)=.*/\
\1.\2.\3=starting time in hh.mm.ss/p
#=end date
/=starting date/ s/[0-9]*\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)=.*/\
\1.\2.\3=end time in hh.mm.ss/p
' YourFile
自我评论(linux GNU sed中的posix版本so'--posix')
它主要是从第行重新格式化信息AWK&bash版本:
for FNAME in $(ls -1 *.dav)
do
channel=$(echo ${FNAME} | awk -F _ '{print $2}')
starttime=$(echo ${FNAME} | awk -F _ '{print $4}')
endtime=$(echo ${FNAME} | awk -F _ '{print $5}')
#
echo ${channel}
#
STARTH=${starttime:8:2}
STARTM=${starttime:10:2}
STARTS=${starttime:12:2}
ENDH=${endtime:8:2}
ENDM=${endtime:10:2}
ENDS=${endtime:12:2}
#
echo ${channel}"_"${STARTH}"."${STARTM}"."${STARTS}"-"${ENDH}"."${ENDM}"."${ENDS}".dav"
mv -f ${FNAME} ${channel}"_"${STARTH}"."${STARTM}"."${STARTS}"-"${ENDH}"."${ENDM}"."${ENDS}".dav"
done
@吉德:我自己经常这样问。