带多行段的sed条件分支
我第一次尝试用sed替换多行。我在那里(和)找到了几个好的指针。将其用作启动程序,我有以下命令:带多行段的sed条件分支,sed,branch,conditional,Sed,Branch,Conditional,我第一次尝试用sed替换多行。我在那里(和)找到了几个好的指针。将其用作启动程序,我有以下命令: sed ' /<dependency>/,/<\/dependency>/ { # Find a set of lines for a dependency s/\(<artifactId>\)m[^<]*\(<\/artifactId>\)/\1ARTIFACTID\2/ # Substitute if artifactId start
sed '
/<dependency>/,/<\/dependency>/ { # Find a set of lines for a dependency
s/\(<artifactId>\)m[^<]*\(<\/artifactId>\)/\1ARTIFACTID\2/ # Substitute if artifactId starts with 'm'
t depend-update # If we substituted, go to depend-update. Otherwise, continue
:depend-unchanged
s/\(<groupId>\)[^<]*\(<\/groupId>\)/\1CHANGE_A\2/ # Change groupId to have 'A'
b # branch to end
:depend-update
s/\(<groupId>\)[^<]*\(<\/groupId>\)/\1CHANGE_B\2/ # Change groupID to have 'B'
b # branch to end
}
' \
inputfile.xml
sed'
//,//{#为依赖项查找一组行
s/\(\)m[^多行是由于在strem/文件输入上逐行而不是作为一个整体使用sed工作的“问题”。
在您的情况下,您处理一组线,但仍然是一次一行,而不是一个块
/startBlock/,/EndBlock/mean just,处理这两个分隔符内的所有行,而不是将块分组到一个大块中
以下是拟议的适应化修改
sed '
/<dependency>/,\#</dependency># {
# load into the buffer
/<dependency>/ h;/<dependency>/ !H
\#</dependency># {
# At end of block, load the buffer and work on it
g
# Find a set of lines for a dependency
s/\(<artifactId>\)m[^<]*\(<\/artifactId>\)/\1ARTIFACTID\2/ # Substitute if artifactId starts with 'm'
t depend-update # If we substituted, go to depend-update. Otherwise, continue
:depend-unchanged
s/\(<groupId>\)[^<]*\(<\/groupId>\)/\1CHANGE_A\2/ # Change groupId to have 'A'
# branch to end
b # branch to end
:depend-update
s/\(<groupId>\)[^<]*\(<\/groupId>\)/\1CHANGE_B\2/ # Change groupID to have 'B'
# branch to end
b
}
}
' \
inputfile.xml
sed'
//,\## {
#加载到缓冲区中
//h;/!h
\## {
#在块的末尾,加载缓冲区并处理它
G
#为依赖项查找一组行
s/\(\)m[^
<dependency>
<groupId>CHANGE_A</groupId>
<artifactId>test.a</artifactId>
</dependency>
<dependency>
<groupId>CHANGE_A</groupId>
<artifactId>ARTIFACTID</artifactId>
</dependency>
<dependency>
<groupId>CHANGE_A</groupId>
<artifactId>test.b</artifactId>
</dependency>
sed '
/<dependency>/,\#</dependency># {
# load into the buffer
/<dependency>/ h;/<dependency>/ !H
\#</dependency># {
# At end of block, load the buffer and work on it
g
# Find a set of lines for a dependency
s/\(<artifactId>\)m[^<]*\(<\/artifactId>\)/\1ARTIFACTID\2/ # Substitute if artifactId starts with 'm'
t depend-update # If we substituted, go to depend-update. Otherwise, continue
:depend-unchanged
s/\(<groupId>\)[^<]*\(<\/groupId>\)/\1CHANGE_A\2/ # Change groupId to have 'A'
# branch to end
b # branch to end
:depend-update
s/\(<groupId>\)[^<]*\(<\/groupId>\)/\1CHANGE_B\2/ # Change groupID to have 'B'
# branch to end
b
}
}
' \
inputfile.xml