用图案替换字符串,保持字符串不变

时间:2018-05-14 05:29:45

标签: shell awk sed

会用sed或awk替换字符串,它会识别下面提到的模式

示例:它会查找以" XX"开头的单词。并以" XX"结束并用" $ {hf:"在" XX"的开始和"}"在" XX"结束时     的 INPUT

CREATE TABLE XX_DB_XX.test_XX_YYYYMMDD_XX
AS
SELECT id
FROM XX_R_DB_XX.usr_XX_YYYYMMDD_XX
WHERE year              = XX_YYYY_XX
   AND month       = XX_MM_XX
   AND day         = XX_DD_XX;

输出

CREATE TABLE ${hf:XX_DB_XX}.test_${hf:XX_YYYYMMDD_XX}
AS
SELECT id
FROM ${hf:XX_R_DB_XX}.usr_${hf:XX_YYYYMMDD_XX}
WHERE year              = ${hf:XX_YYYY_XX}
   AND month       = ${hf:XX_MM_XX}
   AND day         = ${hf:XX_DD_XX};

试图替换模式匹配,但问题是,在输出中我要替换$ A,到子链接" XX _(*)_ XX"输入文件中可用的字符串。

 cat test.hql | gawk '{ print gensub(/XX_+[A-Z,_]+_XX/, "${hiveconf:$A}", 1) 
}' |  gawk '{ print gensub(/XX_+[A-Z]+_XX/, "${hiveconf:$A}", 1) }'

输出: - >我收到的信息需要在可用的字符串中进行更新,那么如何做到这一点:

CREATE TABLE ${hiveconf:$A}.test_${hiveconf:$A}
AS
SELECT id
FROM ${hiveconf:$A}.usr_${hiveconf:$A}
WHERE year              = ${hiveconf:$A}
AND month       = ${hiveconf:$A}
AND day         = ${hiveconf:$A};

3 个答案:

答案 0 :(得分:1)

关注awk可能对您有帮助。

awk '{gsub(/XX_[a-zA-Z]+_XX/,"${hf:&}")} 1'   Input_file

答案 1 :(得分:1)

这就是sed的存在,

sed 's/XX[[:alnum:]_]*XX/${hf:&}/g' file

[[:alnum:]_]代表Alpha数字或下划线。附加*在正则表达式中表示零次或多次。

答案 2 :(得分:1)

或者你可以做到

sed 's/\(XX[^'XX']*XX\)/${hf:\1}/g'

如果在XX之间可能还有非字母数字字符。

首先匹配XX,然后等待直到找到XX。