Shell去除文件中的多余换行符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
shName=`basename $0 .sh`
log_dir=/home/mds/file/log/shelllog/delimiter_deal
log_file=${log_dir}/${shName}_`date +%Y%m%d`.log
Log() { createTime=`date +"%Y-%m-%d %H:%M:%S"` echo "[${createTime}] $*" |tee -a ${log_file} 2>/dev/null }
CheckDir() { if [ ! -d $1 ]; then mkdir_log=` mkdir -p -m 775 $1 ` if [ $? -ne 0 ]; then Log "[$1] create fail!" Log "[$1] does not exist!!please check..." exit 1 fi fi } CheckDir "$log_dir"
deal_file=$1 col_num=$2 if [ $# -ne 2 ];then Log "Params input error, please check..." Log "Eg: sh dealfile_delimiter.sh abs_file_name col_num" exit 1 fi
if [ ! -f $deal_file ];then Log "[$deal_file] not exists ..." exit 1 fi
deal_path=`dirname $deal_file` cd $deal_path if [ $? -ne 0 ];then exit 1 fi Log "切换路径成功,当前路径为: `pwd`" Log "开始处理文件中的多余换行符: ${deal_file}"
file_name=`basename $deal_file`
bak_file=${file_name}.bak`date +%m%d%H%M%S` tmp_file=${file_name}.tmp`date +%m%d%H%M%S` err_file=${file_name}.err`date +%m%d%H%M%S` cp -f -p $deal_file ${bak_file} && chmod 775 ${bak_file} if [ $? -eq 0 ]; then Log "备份原文件成功,备份文件为:${bak_file}" > ${file_name} if [ $? -ne 0 ]; then Log "清空原文件失败..." exit 1 fi Log "清空原文件成功..." else echo "备份文件失败" exit 1 fi
awk -F"[/\x7F][/\x5E]" '{if(NF < '$col_num' && NF >0 ) print $0}' <${bak_file} >${tmp_file} if [ $? -ne 0 ]; then Log "读取文件中少于指定列数且非空行的记录失败..." exit 1 fi Log "读取文件中少于指定列数且非空行的记录成功, 并记录到文件: ${tmp_file}"
Log "开始去除多余换行符,请等待..."
new_line=""; old_line=""; cur_line=""
rownum=0; total_colnum=0; deal_rows=0
for colnum in `awk -F"[/\x7F][/\x5E]" '{print NF}' <${tmp_file}` do rownum=$((rownum+1)) deal_rows=$((deal_rows+1)) err_rows=$((err_rows+1)) total_colnum=$((total_colnum+colnum)) cur_line=`sed -n "$rownum"p <${tmp_file}` new_line="${old_line}${cur_line}"
if [ $((total_colnum-deal_rows+1)) -eq $col_num ]; then echo "$new_line" >>$file_name new_line=""; total_colnum=0; deal_rows=0 else echo "$new_line" >>${err_file} fi old_line=$new_line echo -n ". " done echo ""
Log "去除文件中的多余换行符完成..."
awk -F"[/\x7F][/\x5E]" '{if(NF == '$col_num') print $0}' <${bak_file} >>${file_name} Log "合并正常列数数据完成,处理后文件:${deal_file}"
|
蚂蚁🐜再小也是肉🥩!