有时候我们会遇到这样一种情况:
一些字符串资源要从原始项目A移植到现在我们开发的项目B中
比如移植app名字
<string name="app_label">Calendar</string>
我们需要做的是:
在新项目对应的语言资源中查找是否有app_label这个资源。
有:则查看新旧资源是否一致
一致:则什么也不做
不一致:删除旧的,添加新的资源
没有:添加新的资源
工作内容很简单,但是,语言种类可能达到五六十种,移植的资源往往也不是一两个,所以工作量不可忽视
我觉得这种毫无技术含量的体力活还是交给脚本处理的好,为此特意写了个工具,希望能帮助大家提高效率
使用方法:
1.先将需要移植的资源的key统一放到一个文本中,用换行分隔。
比如:
$ cat /home/su1216/string_list app_name button_name company_name ……
注意:string_list这个文件如果是在windows下制作的,需要先将它转换成unix格式,方法如下:
用vi打开脚本,修改文件格式,命令如下
:set ff=unix
然后保存退出
2.然后执行下面命令即可:
merge_strings android_project_src android_project_dest /home/su1216/string_list
#!/bin/bash #example #merge_strings project_src/packages/apps/Settings project_dest/packages/apps/Settings string_list src_dir="$1" dest_dir="$2" string_list="$3" #确保list文件中含有\n,如果已经含有\n,那么不会再增加 sed -i -e '$a\' "$string_list" regex_with_all_string="" while read line; do regex_with_all_string=$regex_with_all_string"name=\"$line\"|" done < "$string_list" regex_with_all_string=${regex_with_all_string%|*} result_list=`grep -Pr "$regex_with_all_string" $src_dir/res/values*/*.xml` #echo "grep -Pr '"$regex_with_all_string"' $src_dir/res/values*/*.xml" if [ -f "results.txt" ]; then echo "rm results.txt first please." exit fi touch results.txt IFS_OLD=$IFS IFS=$'\n' for line in $result_list; do echo "${line#*res/}" >> results.txt done IFS=$IFS_OLD make_new_xml_file() { local country="$1" local folder=${country%/*} if [ ! -d "$dest_dir/res/$folder" ]; then mkdir "$dest_dir/res/$folder" fi local xml_path="$dest_dir/res/$country" touch "$xml_path" echo '<?xml version="1.0" encoding="utf-8"?>' > "$xml_path" #line1 echo '<resources>' >> "$xml_path" #line2 echo '</resources>' >> "$xml_path" #line3 } insert_line() { #</resources> 插入到这行之前 local string_file="$1" local line="$2" local trim_line=`echo $2 | grep -Po '\S.*\S'` local name=`echo $trim_line | grep -Po "(?<=name=\").*?(?=\")"` local line_no=`grep -n "\b$name\b" "$string_file" | grep -Po "^\d+"` #a.检查是否有这个字段 if [ "$line_no" != "" ]; then #echo "line_no=$line_no" "$string_file" local result=`grep -n "$trim_line" "$string_file"` #b.检查是否能完整匹配。如果不能,则删除旧的,添加新的 if [ "$result" = "" ]; then echo "sed command :""$line_no""d" sed -i "$line_no""d" "$string_file" sed -i '/<\/resources>/i\'"$line" "$string_file" fi else sed -i '/<\/resources>/i\'"$line" "$string_file" fi } #MERGE while read line; do country_new=`echo "$line" | grep -Po "^.*?\.xml"` string_file="$dest_dir/res/$country_new" line=`echo "$line" | grep -Po "(?<=:).*"` if [ ! -f "$string_file" ]; then make_new_xml_file "$country_new" fi #echo "$line" insert_line "$string_file" "$line" done < results.txt
转贴请保留以下链接
本人blog地址
相关推荐
NULL 博文链接:https://lujinan858.iteye.com/blog/437004
bash写的字符串常用函数,这个可以看看。资源分就免了
该文档介绍了shell编程中对字符串处理的一些基本命令, 如sed, cut, awk等等
本文档是自己总结的关于 bash shell 的字符串的操作合集, 并生成了可以复制内容的pdf 以保持排版
在Android上使用Bash替代sh, 详细用法可以参考我的博客http://blog.csdn.net/gmszone/article/details/7246761
Bash工具指南
第一、找出字符或者字符串的类型,是数字、字母还是其他特定字符,是可打印字符,还是不可打印字符(一些控制字符)。 第二、找出组成字符串的字符个数和字符串的存储结构(比如数组)。 第三、对串的常规操作:求子串、...
busybox虽然强大但是没有bash,只有ash和sh,使用起来不够强大,因此在网上查到了如何编译android下的gnu bash。
兰姆杜执行从 AWS Lambda 执行任何 shell 字符串或 bash 脚本安装 npm i --save lambduh-execute用法 var Q = require ( 'q' ) ;var execute = require ( 'lambduh-execute' ) ;//your lambda functionexports . ...
主要介绍了Bash Shell字符串操作总结,包含取长度、截取、查找位置、替换等等,需要的朋友可以参考下
linux bash字符串处理大全,需要的朋友可以参考下
1. 字符串声明 概述 字符串的基本操作 脚本 1 # 声明字符串 str01=str01 echo ${str01} # 单引号也可以 # 不过后面的例子, 通常是用 双引号, 具体原因, 以后会解释\nstr02='str02' echo ${str02} # 对引号的转义,...
BASH中文手册.pdfBASH中文手册.pdf
需要对字符串查找其中某个字符最后出现的位置,这个在PHP (strrpos)或者Perl (rindex)里面都有现成函数可用的功能,在Shell里面居然一时想不出个道道来。在论坛上发贴也没人解答(不知道是问题太简单还是真的很高深...
细绳 用Bash编写的字符串处理程序。使用它source path/to/string/source.bashawk -F : ' {print $5} ' /etc/passwd | string_title可用功能 string_camelcase_underscore string_lower 小写的字符串。 传递字符串...
Bash中文手册,适合Linux shell入门级使用!
Bash脚本美化器 这是Paul Lutus为PyPI(Python软件包索引)编写的Bash脚本美化工具的包装。 Bash脚本美化工具是Bash Shell脚本的...或发送一个字符串到BashBeautify。 bashbeautify - < input.sh > output.sh
中国linux论坛man手册页翻译计划,bash的中文解释,翻译的很不错。对于学习linux脚本编程的同学来说,绝对是不可以没有的。word文档格式。