Bash脚本reg-exp和objective-c

时间:2012-06-20 12:09:31

标签: objective-c regex bash

我是脚本语言的新手,我想为你们提供帮助

我想创建一个名为constant.h的文件,并在特定目录中搜​​索所有文件* .m以获取reg-exp @"LBL_[[0-9]]+" ex:@“LBL_75847”,并将这些匹配写入constant.h如果没有写(没有重复到文件常量),像这样

NSString *const C_LBL_[[0-9]] = thematch; // exp NSString *const C_LBL_78787 = @"LBL_78787";

此外,此脚本还应将所有匹配的@“LBL _ [[0-9]] +”替换为* .m文件,其常量变量为C_LBL _ [[0-9]] +

提前致谢

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

$ cat ocstr.awk

# If the code contains @"LBL_....", replace it with C_LBL_... and
# remember the labels for later.
match($0, /@"LBL_[0-9]*\"/) {
        S=$0; P=""

        do        # Loop until there's nothing left in the right half
        {
                A[L=substr(S, RSTART+2, RLENGTH-3)]++; # Remember label
                P=P substr(S, 1, RSTART-1) "C_" L;
                S=substr(S, RSTART+RLENGTH);
        }
        while(match(S, /@"LBL_[0-9]*\"/)); # Keep hunting for more labels

        $0=P S; # Update the line to be printed
}

# If we are given file.pm, print into file.m
{       O=FILENAME;
        sub(/[.]pm$/, ".m", O);
        print > O;      }

END {
        for(L in A) printf("NSString *const C_%s = @\"%s\";\n", L, L);
}

$ awk -f ocstr.awk dir/*.pm > list

$ cat list

NSString *const C_LBL_10011 = @"LBL_10011";
NSString *const C_LBL_10012 = @"LBL_10012";
NSString *const C_LBL_10804 = @"LBL_10804";
NSString *const C_LBL_10000 = @"LBL_10000";

$ cat dir/*.m

tableviewlogin.backgroundView = nil;
    [welcometitle setFont:[UIFont fontWithName:FONTB size:FONTSIZETITLE]];
    [welcometitle setText:[MSharedFunctions UF8ErrorMessageForCode:C_LBL_10000]];
    [welcometitle setTextAlignment:UITextAlignmentCenter];
    NSString *buttontitle = [MSharedFunctions UF8ErrorMessageForCode:C_LBL_10012];
    [forgotpassword setTitle:buttontitle forState:UIControlStateNormal];
    [forgotpassword setTitle:buttontitle forState:UIControlStateHighlighted];
    [self.navigationItem setTitle:[MSharedFunctions UF8ErrorMessageForCode:C_LBL_10011]];
    [[self tabBarItem] setTitle:[MSharedFunctions UF8ErrorMessageForCode:C_LBL_10804]];

$