我有一个非结构化文件,我想搜索和替换字符串模式。
必须替换在String和String之间存在的字符串;该模式之外的字符串应该保持原样。
文件格式如下
col4 is required to be upper so
make col4 upper
abc 12345 !$% DATA SELECT
col1 as col1,
col2 as col2.
col3,
sch.col4 as col4,
sch.tab.col4 as col4_1,
col4,
col5 FROM sch.tab
xyz 34354 ^&* DATA SELECT
col5 as col5,
col3,
col4,
col4 as col4,
col4 FROM
blah blah blah
我想替换:
col4,
和upper(col4) as col4,
sch.col4
和upper(sch.col4)
sch.tab.col4
和upper(sch.tab.col4)
col4
(如果col4位于select查询的末尾)使用upper(col4) as col4
该文件在Linux服务器上,我尝试使用sed和awk缩小包含col4的行,但无法从那里向前移动。
我能用下面的方法识别出一种模式
awk '/SELECT/,/FROM/' test_file.txt | awk '/col4/{print $0, NR}' | awk -F AS '{print $1}'
查找“选择”和“来自”之间的文本
找出有COL4的线路
打印第一个字段
sed -n -e '/SELECT/,/FROM/p' -e 's/\(\([a-zA-Z]\{1,\}\.\)\{0,\}\)col4/upper(\0)/g' test_file.txt
使用sed
实际:
col4 is required to be upper so
make col4 upper
abc 12345 !$% DATA SELECT
col1 as col1,
col2 as col2.
col3,
sch.col4 as col4,
sch.tab.col4 as col4_1,
col4,
col5 FROM sch.tab
xyz 34354 ^&* DATA SELECT
col5 as col5,
col3,
col4,
col4 as col4,
col4 FROM
blah blah blah
预期结果:
col4 is required to be upper so
make col4 upper
abc 12345 !$% DATA SELECT
col1 as col1,
col2 as col2.
col3,
upper(sch.col4) as col4,
upper(sch.tab.col4) as col4_1,
upper(col4) as col4,
col5 FROM sch.tab
xyz 34354 ^& DATA SELECT
col5 as col5,
col3,
upper(col4) as col4,
upper(col4) as col4,
upper(col4) as col4 FROM
blah blah blah
非常感谢您的帮助!啊!
最佳答案
我认为,至少95%的人这样做了。如果有错误,请告诉我:
with open('ej.txt', 'r') as file:
string=file.read().replace('\n',' ')
import re
matches=re.findall(r'SELECT.*?FROM',string)
replacements={"col4,":"upper(col4) as col4,",
"sch.col4":"upper(sch.col4)",
"sch.tab.col4":"upper(sch.tab.col4)",
"col4 as col4,": "upper(col4) as col4,"}
new_matches=[]
for match in matches:
for k,v in replacements.items():
match=match.replace(k,v)
new_matches.append(match)
for k,v in {k:v for k,v in zip(matches,new_matches)}.items() :
string=string.replace(k,v)
string
关于python - 多行搜索和替换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56189622/