![](/img/trans.png)
[英]Notepad++ Removing text between two strings if third string is present between other strings
[英]Notepad++ replace text between two strings spawning multiple lines and retain some part of the string in between
我使用 show create table 命令提取了许多 hive 表。
output是这样的:
CREATE EXTERNAL TABLE MYSCHEMA.MyTABLE(
`col1` string,
`col2` string)
PARTITIONED BY (
`data_as_of_date` string)
ROW FORMAT SERDE
'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'
WITH SERDEPROPERTIES (
'input.regex'='^(.*?)~}\\|(.*?)~}\\|(.*?)$')
STORED AS INPUTFORMAT
'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'
LOCATION
'/mnt/data/schema/layer/domain/MYTABLE'
TBLPROPERTIES (
'DO_NOT_UPDATE_STATS'='true',
'STATS_GENERATED_VIA_STATS_TASK'='true',
'last_modified_by'='user',
'last_modified_time'='1603077305',
'numRows'='23483974',
'parquet.compression'='SNAPPY',
'transient_lastDdlTime'='1608243340');
我想替换...之间的文字
ROW FORMAT SERDE
'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'
WITH SERDEPROPERTIES (
'input.regex'='^(.*?)~}\\|(.*?)~}\\|(.*?)$')
STORED AS INPUTFORMAT
'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'
LOCATION
'/mnt/data/schema/layer/domain/MYTABLE'
TBLPROPERTIES (
'DO_NOT_UPDATE_STATS'='true',
'STATS_GENERATED_VIA_STATS_TASK'='true',
'last_modified_by'='user',
'last_modified_time'='1603077305',
'numRows'='23483974',
'parquet.compression'='SNAPPY',
'transient_lastDdlTime'='1608243340');
...至...
STORED AS PARQUET
LOCATION '/mnt/data/schema/layer/domain/MYTABLE'
TBLPROPERTIES('parquet.compression'='SNAPPY');
...使用记事本++。
在这里,如果您观察到,LOCATION 参数应与原始参数保持一致,并且应如上所述更换 rest。 基本上,替换是跨多行产生的,我还保留了部分文本。 有人请指导我可以在 Notepad++ (v7.8.2) 中使用的正则表达式。
最终结果应如下所示:
CREATE EXTERNAL TABLE MYSCHEMA.MyTABLE(
`col1` string,
`col2` string)
PARTITIONED BY (
`data_as_of_date` string)
STORED AS PARQUET
LOCATION '/mnt/data/schema/layer/domain/MYTABLE'
TBLPROPERTIES('parquet.compression'='SNAPPY');
有许多表,每个表都有不同的 LOCATION 参数。 不希望如上所述更换 LOCATION。
如果我能分两部分做到这一点也很好。 首先替换 LOCATION 上方的所有内容,然后替换 TBLPROPERTIES(如果不能在单个正则表达式中完成)。
ROW FORMAT SERDE[\s\S]+?(LOCATION\s+.+\R)[\s\S]*?TBLPROPERTIES[^)]+?\);
STORED AS PARQUET \n$1TBLPROPERTIES\('parquet.compression'='SNAPPY'\);
. matches newline
. matches newline
解释:
ROW FORMAT SERDE # literally
[\s\S]+? # 1 or more any character, including newline, not greedy
( # group 1
LOCATION # literally
\s+ # 1 or more spaces
.+ # 1 or more any character but newline
\R # any kind of linebreak
) # end group
[\s\S]*? # 1 or more any character, including newline, not greedy
TBLPROPERTIES # literally
[^)]+? # 1 or more any character that is not closing parenthesis
\); # closing parenthesis and semicolon
替代品:
STORED AS PARQUET
\n
$1
TBLPROPERTIES\('parquet.compression'='SNAPPY'\);
截图(之前):
截图(之后):
我可以使用两个单独的查找和替换正则表达式来做同样的事情。 不知道它会如此简单,只需 2 次查找和替换。
将: ROW FORMAT SERDE.*?LOCATION
替换为: STORED AS PARQUET\r\nLOCATION
将: TBLPROPERTIES.*?\)
替换为: TBLPROPERTIES \(\r\n 'parquet.compression'='SNAPPY'\)
我在单个正则表达式中很难做到这一点。 任何人?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.