![](/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.