批量修改文件后缀
1、shell实现
#!/bin/bash
cd '/home/dccmc/Tool_Management_Platform/auto_check/zsj/shell/data'
pwd
read -p "input the old file:" old
read -p "input the new file:" new
[ -z ${old} ] || [ -z ${new} ] && echo "error" && exit #判断用户是否有输入,如果没有输入打印error并退出
for file in `ls *.${old}`
do
if [ -f ${file} ];then
rename .${old} .${new} *
fi
done
2、python实现
import os
def file_rename():
path = input("请输入对应的路径(例如:F:\\test\):")
old_suffix = input("请输入你要修改的文件的后缀(记得加.):")
new_suffix = input("请输入你要改成的文件的后缀(记得家.):")
file_list = os.listdir(path)
for file in file_list:
old_dir = os.path.join(path, file)
print('当前文件:', file)
if os.path.isdir(old_dir):
continue
if old_suffix != os.path.splitext(file)[1]:
continue
filename = os.path.splitext(file)[0]
new_dir = os.path.join(path, filename + new_suffix)
os.rename(old_dir, new_dir)
print('修改后的文件:', new_dir)
if __name__ == '__main__':
file_rename()
3、文件中内容去重
awk '!a[$0]++' filename.txt > newfilename.txt
cat filename.txt|sort -u >newfilename.txt
4、自动部署脚本
. /home/dccmc/.bash_profile
# 判断文件是否存在,如果不存在则 创建
path=/home/dccmc/Tool_Management_Platform/auto_check/zsj/shell/sh
filename=/home/dccmc/Tool_Management_Platform/auto_check/zsj/shell/sh/zb_info.sh
[ ! -d ${path} ] && mkdir -p ${path}
chmod -R 775 ${path}
[ ! -f ${filename} ] && touch ${filename}
# [ -z $1 ] && exit
chmod -R 775 ${filename}
(
cat <<'EOF'
. /home/dccmc/.bash_profile
ll_date=`date -d last-day +%Y%m%d`
tbill_date=`date +%Y%m%d`
prov=()
n=0
for ((i=0;i<${n};i++));
do
file_path=/ogsapp/data/fs01/appcdr/file/${prov[i]}/all/bak/Data/NORMAL_FILE_OUTPUT
file_count=`cat $file_path/GPP*$bill_date*REQ $file_path/GPP*$tbill_date*REQ|grep ",$bill_date,"|grep -v grep|wc -l`
echo "{\"kafka_billing_count\":$file_count}" >/ogsapp/tmp/shell/xxjf/${prov[i]}_"$bill_date"_kafka_billing_count.txt
if [ $file_count -ne 0 ];then
echo "{\"total_traffic\":`cat $file_path/GPP*$bill_date*REQ $file_path/GPP*$tbill_date*REQ|grep ",$bill_date,"|grep -v grep|awk -F, '{sum += ($22 +$23)};END{print sum}'`}"
>/ogsapp/tmp/shell/xxjf/${prov[i]}_"$bill_date"_total_traffic.txt
else
echo "{\"total_traffic\":$file_count}" >/ogsapp/tmp/shell/xxjf/${prov[i]}_"$bill_date"_total_traffic.txt
fi
done
EOF
) > ${filename}