用sed或awk替换字符串,在其中它标识出一个模式,如下所述

时间:2018-09-24 15:55:18

标签: awk sed

请让我知道如何使用AWK将下面的INPUT转换为提到的OUTPUT:

输入

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

输出

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;

下面是我用来从给定的OUTPUT转换为INPUT的东西。

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

相反,我尝试了以下方法,但没有解决

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

1 个答案:

答案 0 :(得分:3)

sed将在此处

$ sed -E 's/\$\{hf:([^}]+)\}/\1/g' file

与GNU awk

类似
$ awk '{print gensub(/\${hf:([^}]+)}/,"\\1","g")}' file