shell脚本从文件中读取数据按照特定的格式写文件

时间:2021-08-16
本文章向大家介绍shell脚本从文件中读取数据按照特定的格式写文件,主要包括shell脚本从文件中读取数据按照特定的格式写文件使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

具体脚本如下:

#!/bin/bash

setParams(){
sed -i '$d' $3
echo $2 | tr -d '[]'|awk -F, '{
for(i=1;i<=NF;i++)
if(match($i,/ENDTIME/)||match($i,/STARTTIME/)){
if(length(substr($i,index($i,":")))==10)
{
sub(/:.*/,",TimeUtils.getYM(0,0)",$i);
print $i;
}
else if(length(substr($i,index($i,":")))==13)
{
sub(/:.*/,",TimeUtils.getYMD(0,0,-1)",$i);
print $i
}
else if(length(substr($i,index($i,":")))==7)
{
sub(/:.*/,",TimeUtils.getY(0)",$i);
print $i
}
}else{
sub(/:/,",",$i);
print $i
}
}' | awk 'BEGIN{
FS="\r"
print " public static final HashMap<String,String> '"$1"' = new HashMap<String,String>{{"
}
{
for(j=1;j<=NF;j++)
print " put("$j");"
}
END{ print " }};" }' >> $3
sed -i '$a\}' $3
}

addParamInfile(){
mapVar=$(sed -n '/'"$1"'[ =]/p' $2)
echo "=========start exec addParamInfile========="
if [[ -z $mapVar ]]; then
echo "+++++start insert paramters ${1}."
setParams $1 $3 $2
cat $2
echo "+++++end insert paramters ${1}."
else
echo "$1 already exists."
fi
echo "=========end exec addParamInfile========="
}

createMapFile(){
echo "=========start exec createMapFile========="
mapFileDirPath=${1}"/"${2}
echo "create new folder $mapFileDirPath ..."
mkdir $mapFileDirPath
fullFileName=${mapFileDirPath}"/"${3}".java"
echo "create new file $fullFileName ..."
echo -e "public static $3{\n}">$fullFileName
cat $fullFileName
echo "=========end exec createMapFile========="
}

printTitle(){
echo
echo "###################################################################################"
echo "# #"
echo "# The Time Of Running Scrpit:$(date) #"
echo "# #"
echo "###################################################################################"
echo
}

addUrl(){
urlnametemp=$(sed -n '/'"${2}"'[ =]/p' $1 | cut -d = -f 2 | tr -d '"')
urlPathinfile=$(echo ${urlnametemp%;*})
echo "========Insert Or Modify urlPath information=========="
echo "Modify $1"
if [[ -z $urlPathinfile ]]; then
sed -i '/}/i\ public static final '"$2"'="'"$3"'";' $1
echo "Finish to insert $2 ."
else
if [[ "$urlPathinfile" == "$3" ]]; then
echo "$urlPathinfile already exists."
else
echo "Modify $urlPathinfile ..."
sed -i '/'"${2}"'[ =]/s#'"$urlPathinfile"'#'"$3"'#g' $1
fi
fi
}

addParameters(){
echo "========Insert Or Modify urlParameters information=========="
#api参数存储文件名称
fileName=$(echo $2|tr [a-z] [A-Z])"_inforMap"
#api所属看板缩写目录名称,全路径
mapFileDirPath=$(find $1 -iname $2)
echo "The folder is $mapFileDirPath"
#判断目录是否存在;存在时,进行下一步处理;不存在时,新建目录、文件
if [[ -d $mapFileDirPath ]]; then
echo "$mapFileDirPath already exists."
fullFileName=$(find $1 -iname ${fileName}"*")
echo "目录文件已存在,插入新的api参数常量."
addParamInfile $urlName $fullFileName $urlPara
else
echo "目录文件不存在,新建目录文件."
createMapFile $1 $2 $fileName
echo "新增api的参数常量"
addParamInfile $3 $fullFileName $4
fi
}

if [[ -z $1 ]]; then
echo "Usage: bash addUrlParamUtil.sh 数据读取文件"
elif [[ -f $1 ]]; then
printTitle>>informaputil.log
mapDir=$(find . -iname "mapinfo")
urlFileName=$(find . -iname "urlmap*")
count=1

while read urlInfor
do
echo
echo "#######################执行第$count次########################"
#api的名称
urlName=$(echo $urlInfor|awk 'BEGIN{FS="[ \t]"} {print $2}')
#api的url参数
urlPara=$(echo $urlInfor|awk 'BEGIN{FS="[ \t]"} {print $4}')
#api所属看板缩写,即目录名称
mapFileDir=$(echo $urlInfor|awk 'BEGIN{FS="[ \t]"} {print $1}')
#api的url
urlPath=$(echo $urlInfor|awk 'BEGIN{FS="[ \t]"} {print $3}')
#新增api的url
addUrl $urlFileName $urlName $urlPath
#新增api的入参
addParameters $mapDir $mapFileDir $urlName $urlPara
let count=$count+1
done<$1>>informaputil.log
else
echo "$1 is not a file."
fi

原文地址:https://www.cnblogs.com/zhangtec/p/15149982.html