Linux shell multifile content replace with sed

#!/bin/bash


#                  Linux shell multifile content replace with sed
# 声明:
#     本源代码主要是利用两份(中、英文)具有相同键值对的json数据,对html内的中文进行
# 自动化文本替换的代码。
#
#                                        2015-11-20 晴 深圳 南山平山村 曾剑锋


# 得到中文部分
sed -n "/\"/p" SimpChinese.txt | grep -Po -e "\"\s?:\s?.*" | grep -Po -e ":\s?.*" | grep -o -e "\".*\"" > SimpChinese

while read chinese
do

    # 获取中文对应的id部分
    id=`grep "$chinese" SimpChinese.txt | grep -o -e "^.*:" | grep -o -e "\".*\"" | head -n 1`
    # 由id获取英文的部分
    english=`grep "$id" English.txt | grep -Po -e "\"\s?:\s?.*" | grep -Po -e ":\s?.*" | grep -o -e "\".*\"" | head -n 1`

    # 剔除双引号
    chinese=${chinese:1:-1}
    english=${english:1:-1}

    # 去掉两边的空格
    #chinese=`echo $chinese | sed "s/ *$//g"| sed "s/^ *//g"`

    # 追加到文件末尾
    echo "$chinese===$english" >> translate.txt

done < SimpChinese

# 将有些不在json键值对里的中文翻译加入translate.txt中
cat manualTranslate.txt >> translate.txt

while read ChEn 
do

    # 获取中文,由于有些中文字符串有'/',并对其进行处理,主要是为了处理sed中'/'的特殊性
    chinese=`echo $ChEn | grep -o "^.*===" | sed "s/===//" | sed "s/ *$//g" | sed "s/^ *//g" | sed "s^\/^\\\\\\\\/^g"`
    english=`echo $ChEn | grep -o "===.*$" | sed "s/===//" | sed "s^\/^\\\\\\\\/^g"`

    # 匹配并修改html页面,[::!!??]是根据html中字符串的特征添加的内容
    sed -i -e "s/>\s*${chinese}[::!!??]\s*</>${english}</" rootfs/index.htm
    sed -i -e "s/>\s*${chinese}[::!!??]\s*</>${english}</" rootfs/set.htm
    sed -i -e "s/>\s*${chinese}[::!!??]\s*</>${english}</" rootfs/html/*

done < translate.txt


# 部分English.txt
# {
# "Right Limit" : "Set Right Limit",
# "Set Failed!": "Failed to set!",
# "Ok" : "Ok",
# "Cancel" : "Cancel",
# "Set Path" : "StoragePath",
# "Set PTZ" :" PTZ Settings ",
# "Auto-Scan" : "Scan",
# "Left Limit": "Set Left Limit",
# "Add" : "Add ",
# "Preset": "Preset",
# "Path" : "Path",
# "Browse":"Choose",
# "Set Succeeded!" :"Set successfully!",
# "Delete" : "Delete"
# }


# 部分SimpChinese.txt
# {
# "Set Path" : "设置存储路径",
# "Path" : "路径",
# "Browse" : "选择",
# "Set Succeeded!" : "设置成功!",
# "Left Limit" : "设置左边界 ",
# "Right Limit" : "设置右边界 ",
# "Preset" : "预置点 ",
# "Cancel" : "取消",
# "Set PTZ" : "云台设置 ",
# "Auto-Scan" : "线性扫描 ",
# "Delete" : "删除 "
# "Add" : "添加 ",
# "Set Failed!" : "设置失败!",
# "Ok" : "确定",
# }

 

posted on 2015-11-20 08:17  zengjf  阅读(362)  评论(0编辑  收藏  举报

导航