cat batch_modify_file.sh
#!/bin/bash
#by lrj 20181110

#收集IP后的文件
file2=$PWD/file2.txt
touch $file2

#检查文件属性
Checking_file(){
read -p "Please input file name:" file1
u1=find / -type f -name "$file1"|xargs ls -l|awk '{print $3}'
u2=echo $USER
[ $u1 = $u2 ] && continue || echo "登陆用户非文件所有者,没有权限修改,文件所有者为:$u1"
dir_file1=find / -type f -name "$file1"
echo "文件全路径为:$dir_file1"
}
#抽取IP
Extract_IP(){
cat $dir_file1|grep -E -o "([0-9]{1,3}[.]){3}[0-9]{1,3}"|sort|uniq -c|sort -rn|awk '{print $NF}' > $file2
echo "chang IP list:"
cat $file2
}
#替换处理
Replace_IP(){
for line in cat $file2
do
ip1=echo $line
sleep 1
echo "source_IP(原IP): $ip1"
for i in line
do
read -p "dist_IP(替换的IP,如不替换请输入原IP):" ip2
[ $ip1 != $ip2 ] && sed -i "s/$ip1/$ip2/g" $dir_file1 || break
done
done
}

#主函数
main(){
Checking_file
sleep 1
Extract_IP
sleep 1
Replace_IP
}
#调用
main
rm -rf $file2