-1

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

我想创建一个名为 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 文件,其常量 var 为 C_LBL_[[0-9]]+

提前致谢

4

1 回答 1

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]];

$
于 2012-06-22T09:28:22.150 回答