智能sortingLocalized.strings文件

在我Localizable.Strings我尝试所有对按字母顺序。 是否有可能按字母顺序重新排列我的Localizable.strings ? Maby使用genstring或特殊的bash脚本?

在这里我有额外的要求来完成:

1.订购应不区分大小写。

2.第一个X(例如五个)行应该被复制,而不是有序的。

这个要求应该被满足,因为在Localized.strings文件中我有作者,公司名称和产品名称作为注释在上面。

3.保留意见

我想保留对翻译后的string的注释,并在每个翻译之间保留新的行。 这个注释是通过iOS开发人员的特殊的genstrings命令(例如find ./ -name "*.m" -print0 | xargs -0 genstrings -o en.lproj查找所有NSLocalizedString(@"Param",@"Comment") in我的代码并生成对/* Comment */ /r/n "Param" = "Param";文件)。 翻译前的注释行是可选的,可能只有1行。 例如文件:

 /* This is Billy */ "Billy" = "The smartest guy in the univererse"; /* The Hitchhiker's Guide to the Galaxy */ "42" = "the answer to life the universe and everything"; "Johny" = "Johny"; /* Optional field */ "Anny" = "Anny"; 

输出应该是:

 /* The Hitchhiker's Guide to the Galaxy */ "42" = "the answer to life the universe and everything"; /* Optional field */ "Anny" = "Anny"; /* This is Billy */ "Billy" = "The smartest guy in the univererse"; "Johny" = "Johny"; 

这个问题是更复杂的变体或我自己的问题,你可以在这里find: 重新sorting.strings文件

认为这是你想要的
在awk中

 awk 'BEGIN{RS="";FS="\n"} {t=$NF} match(t,/^"([^"]+)/,a){ key[NR]=tolower(a[1])"\t"++x b[x]=$0 } END { asort(key) for (i=1; i<=x; i++) { split(key[i],a,"\t") print b[a[2]] "\n" } }' file 

产量

 /* The Hitchhiker's Guide to the Galaxy */ "42" = "the answer to life the universe and everything"; /* Optional field */ "Anny" = "Anny"; /* This is Billy */ "Billy" = "The smartest guy in the univererse"; "Johny" = "Johny"; 

编辑

跳过前5行并仍然打印

 awk 'NR<6{print;next} NR==6{RS="";FS="\n"} {t=$NF} match(t,/^"([^"]+)/,a){ key[NR]=tolower(a[1])"\t"++x b[x]=$0 } END { asort(key) for (i=1; i<=x; i++) { split(key[i],a,"\t") print b[a[2]] "\n" } }' file 

编辑2

我认为这应该适用于Mac电脑

 awk 'NR<6{print;next} NR==6{RS="";FS="\n"} {t=$NF} split(t,a,"\""){ key[NR]=tolower(a[2])"\t"++x b[x]=$0 } END { asort(key) for (i=1; i<=x; i++) { split(key[i],a,"\t") print b[a[2]] "\n" } }' file 

这是另一种方式。

 X=5; file=<file>; \ head -n $X $file && \ cat $file | sed '1,'$X'd' | \ sed 's/\([^;]\)$/\1@@@/g' | \ tr -d '\n' | \ tr ';' '\n' | \ sed 's/$/;/g' | \ awk -F "@@@" '{print $2"@@@"$1}' | \ sed 's/^@@@//g' | \ sort --ignore-case | \ awk -F "@@@" '{print $2"\n"$1"\n"}' | \ cat -s 

解释。

 X=5; file=<file>; \ # define variables head -n $X $file && \ # output first set of lines cat $file | sed '1,'$X'd' | \ # process rest of the lines sed 's/\([^;]\)$/\1@@@/g' | \ # append @@@ to lines not ending with semicolon tr -d '\n' | \ # remove all new lines and make a single line string tr ';' '\n' | \ # break single string into multiple lines at semicolons sed 's/$/;/g' | \ # add semicolons at the end of lines awk -F "@@@" '{print $2"@@@"$1}' | \ # swap comment and translation sed 's/^@@@//g' | \ # remove extra @@@ of translations without comments sort --ignore-case | \ # sort awk -F "@@@" '{print $2"\n"$1"\n"}' | \ # swap translation and comment, print with new lines cat -s # remove extra new lines