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

时间:2018-04-11 23:41:01

标签: bash hive hiveql

以下是我所拥有的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

如何逃避最后几行中的[]-^符号?我已经尝试将\放在所有这些之前,但这不起作用......

编辑:我收到了以下Hive错误,该错误在我添加上述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

然而奇怪的是,如果我直接在Hive中输入相同的查询并运行它,它运行正常。这就是为什么我觉得有一些我不能满足的Bash逃避错误。

2 个答案:

答案 0 :(得分:0)

这些角色不需要在Bash上下文中转义。如果您将hive替换为cat,则可以获得此信息:

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语法错误,请提供您正在使用的数据库和错误消息。

答案 1 :(得分:0)

修改where子句以使用or而不是case表达式。目前,模式并未按预期传递case

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