多行搜索和替换 [英] Multi-line search and replace

查看:85
本文介绍了多行搜索和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非结构化文件,我想搜索并替换字符串模式.

I have an unstructured file and I would like to search and replace pattern of strings.

  • 必须替换SELECT和FROM字符串之间的字符串;这个模式之外的人应该保持原样.

文件格式就像

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.col4upper(sch.col4)
  • sch.tab.col4upper(sch.tab.col4)
  • col4(如果col4在选择查询的末尾)与upper(col4) as col4
  • col4, with upper(col4) as col4,
  • sch.col4 with upper(sch.col4)
  • sch.tab.col4 with upper(sch.tab.col4)
  • col4(if col4 is at the end of select query) with upper(col4) as col4

该文件位于linux服务器上,我尝试使用sed和awk缩小包含col4的行,但无法从那里向前移动.

The file is on linux server and I tried using sed and awk to narrow down the lines containing col4 but could not move forward from there.

我可以使用以下内容识别一种模式

I was able to identify one pattern using below

awk '/SELECT/,/FROM/' test_file.txt | awk '/col4/{print $0, NR}' | awk -F AS '{print $1}' 

在SELECT和FROM之间找到文本
标识具有col4
的行 打印第一个字段

Find the text between SELECT and FROM
Identify the lines that have col4
print the first field

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

非常感谢您的帮助!

推荐答案

使用sed:

sed '/SELECT/,/FROM/ {s/as col4 *//;s/\([A-Za-z]*\.\)\{0,\}col4/upper(&) as col4/;}' file

说明:

  • s/as col4 *//:删除现有的as col4以防止第二次替换后出现重复
  • \([A-Za-z]*\.\)\{0,\}col4:搜索0个或更多字母和点的组合,后跟col4
  • upper(&) as col4/;:替换为新文本(使用&插入匹配字符串)
  • s/as col4 *//: existing as col4 is removed to prevent duplicates after second substitution
  • \([A-Za-z]*\.\)\{0,\}col4: search for 0 or more combinations of letters and dots followed by col4
  • upper(&) as col4/;: replace with new text(matching string is inserted using &)

这篇关于多行搜索和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆