1. 程式人生 > 其它 >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