如何在bash脚本中转义方括号和连字符

mzillmmw  于 2021-06-26  发布在  Hive
关注(0)|答案(2)|浏览(417)

以下是我拥有的bash脚本的代码片段:

APP_ID="get_json_object(mobile_attributes, '$.id')"
PLATFORM="parse_user_agent(user_agent, 'os')"

hive << EOF

INSERT OVERWRITE TABLE new_mobile_app_ids
SELECT
   t.app_id,
FROM
(
    SELECT
       ${APP_ID} as app_id,
    FROM
       sourcetable
    WHERE
       ${APP_ID} RLIKE
         CASE WHEN ${PLATFORM} = 'IOS' THEN
           '[0-9]'
         ELSE
           '[^0-9]'
         END
) t;

EOF

我怎样才能逃过这场灾难 [ , ] , - , ^ 最后几行的符号?我试过 \ 在所有人面前,但那不起作用。。。
编辑:我得到下面的配置单元错误,它开始出现后,我添加了上述 WHERE 条款:

NoViableAltException(13@[323:1: atomExpression : ( ( KW_NULL )=> KW_NULL -> TOK_NULL | ( constant )=> constant | castExpression | caseExpression | whenExpression | ( functionName LPAREN )=> function | tableOrColumn | LPAREN ! expression RPAREN !);])
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser$DFA32.specialStateTransition(HiveParser_IdentifiersParser.java)
    at org.antlr.runtime.DFA.predict(DFA.java:80)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.atomExpression(HiveParser_IdentifiersParser.java:6178)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceFieldExpression(HiveParser_IdentifiersParser.java:6384)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceUnaryPrefixExpression(HiveParser_IdentifiersParser.java:6769)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceUnarySuffixExpression(HiveParser_IdentifiersParser.java:6829)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceBitwiseXorExpression(HiveParser_IdentifiersParser.java:7013)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceStarExpression(HiveParser_IdentifiersParser.java:7173)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedencePlusExpression(HiveParser_IdentifiersParser.java:7333)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceAmpersandExpression(HiveParser_IdentifiersParser.java:7484)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceBitwiseOrExpression(HiveParser_IdentifiersParser.java:7635)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceEqualExpression(HiveParser_IdentifiersParser.java:8165)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceNotExpression(HiveParser_IdentifiersParser.java:9178)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceAndExpression(HiveParser_IdentifiersParser.java:9297)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceOrExpression(HiveParser_IdentifiersParser.java:9456)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.expression(HiveParser_IdentifiersParser.java:6106)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.whenExpression(HiveParser_IdentifiersParser.java:4700)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.atomExpression(HiveParser_IdentifiersParser.java:6266)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceFieldExpression(HiveParser_IdentifiersParser.java:6384)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceUnaryPrefixExpression(HiveParser_IdentifiersParser.java:6769)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceUnarySuffixExpression(HiveParser_IdentifiersParser.java:6829)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceBitwiseXorExpression(HiveParser_IdentifiersParser.java:7013)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceStarExpression(HiveParser_IdentifiersParser.java:7173)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedencePlusExpression(HiveParser_IdentifiersParser.java:7333)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceAmpersandExpression(HiveParser_IdentifiersParser.java:7484)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceBitwiseOrExpression(HiveParser_IdentifiersParser.java:7635)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceEqualExpression(HiveParser_IdentifiersParser.java:8291)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceNotExpression(HiveParser_IdentifiersParser.java:9178)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceAndExpression(HiveParser_IdentifiersParser.java:9328)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.precedenceOrExpression(HiveParser_IdentifiersParser.java:9456)
    at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.expression(HiveParser_IdentifiersParser.java:6106)
    at org.apache.hadoop.hive.ql.parse.HiveParser.expression(HiveParser.java:46050)
    at org.apache.hadoop.hive.ql.parse.HiveParser_FromClauseParser.searchCondition(HiveParser_FromClauseParser.java:6638)
    at org.apache.hadoop.hive.ql.parse.HiveParser_FromClauseParser.whereClause(HiveParser_FromClauseParser.java:6546)
    at org.apache.hadoop.hive.ql.parse.HiveParser.whereClause(HiveParser.java:45943)
    at org.apache.hadoop.hive.ql.parse.HiveParser.selectStatement(HiveParser.java:41673)
    at org.apache.hadoop.hive.ql.parse.HiveParser.regularBody(HiveParser.java:41532)
    at org.apache.hadoop.hive.ql.parse.HiveParser.queryStatementExpressionBody(HiveParser.java:40543)
    at org.apache.hadoop.hive.ql.parse.HiveParser.queryStatementExpression(HiveParser.java:40413)
    at org.apache.hadoop.hive.ql.parse.HiveParser_FromClauseParser.subQuerySource(HiveParser_FromClauseParser.java:5308)
    at org.apache.hadoop.hive.ql.parse.HiveParser_FromClauseParser.fromSource(HiveParser_FromClauseParser.java:3742)
    at org.apache.hadoop.hive.ql.parse.HiveParser_FromClauseParser.joinSource(HiveParser_FromClauseParser.java:1874)
    at org.apache.hadoop.hive.ql.parse.HiveParser_FromClauseParser.fromClause(HiveParser_FromClauseParser.java:1519)
    at org.apache.hadoop.hive.ql.parse.HiveParser.fromClause(HiveParser.java:45944)
    at org.apache.hadoop.hive.ql.parse.HiveParser.selectStatement(HiveParser.java:41646)
    at org.apache.hadoop.hive.ql.parse.HiveParser.regularBody(HiveParser.java:41360)
    at org.apache.hadoop.hive.ql.parse.HiveParser.queryStatementExpressionBody(HiveParser.java:40543)
    at org.apache.hadoop.hive.ql.parse.HiveParser.queryStatementExpression(HiveParser.java:40413)
    at org.apache.hadoop.hive.ql.parse.HiveParser.execStatement(HiveParser.java:1720)
    at org.apache.hadoop.hive.ql.parse.HiveParser.statement(HiveParser.java:1142)
    at org.apache.hadoop.hive.ql.parse.ParseDriver.parse(ParseDriver.java:202)
    at org.apache.hadoop.hive.ql.parse.ParseDriver.parse(ParseDriver.java:166)
    at org.apache.hadoop.hive.ql.Driver.compile(Driver.java:397)
    at org.apache.hadoop.hive.ql.Driver.compile(Driver.java:309)
    at org.apache.hadoop.hive.ql.Driver.compileInternal(Driver.java:1141)
    at org.apache.hadoop.hive.ql.Driver.runInternal(Driver.java:1189)
    at org.apache.hadoop.hive.ql.Driver.run(Driver.java:1078)
    at org.apache.hadoop.hive.ql.Driver.run(Driver.java:1068)
    at org.apache.hadoop.hive.cli.CliDriver.processLocalCmd(CliDriver.java:213)
    at org.apache.hadoop.hive.cli.CliDriver.processCmd(CliDriver.java:165)
    at org.apache.hadoop.hive.cli.CliDriver.processLine(CliDriver.java:376)
    at org.apache.hadoop.hive.cli.CliDriver.executeDriver(CliDriver.java:736)
    at org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:681)
    at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:621)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
FAILED: ParseException line 26:0 cannot recognize input near '_object' '(' 'mobile_attributes' in expression specification

然而奇怪的是,如果我直接在配置单元中输入相同的查询并运行它,它运行得很好。这就是为什么我觉得有一些bash逃避错误,我不迎合。

but5z9lq

but5z9lq1#

这些字符不需要在bash上下文中转义。如果你更换 hivecat 你得到这个:

INSERT OVERWRITE TABLE 
SELECT
   t.app_id,
FROM
(
    SELECT
       get_json_object(mobile_attributes, '$.id') as app_id,
    FROM
       sourcetable
    WHERE
       get_json_object(mobile_attributes, '$.id') RLIKE
         CASE WHEN parse_user_agent(user_agent, 'os') = 'IOS' THEN
           '[0-9]'
         ELSE
           '[^0-9]'
         END
) t;

如果您得到一个sql语法错误,那么请包括您使用的数据库和错误消息。

b0zn9rqh

b0zn9rqh2#

修改 where 要使用的子句 or 而不是 case 表情。从目前的情况来看,这个模式并没有像预期的那样通过 case .

WHERE (${PLATFORM} = 'IOS' and ${APP_ID} RLIKE '[0-9]')
OR (${PLATFORM} <> 'IOS' and ${APP_ID} RLIKE '[0-9]')

相关问题