找到上下文后用多行替换多行 [英] Replace multiple lines with multiple lines after finding a context
问题描述
我有一个包含以下几行的文件.
I have a file which contains below lines.
xyz_write =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostname.com)(PORT = 80))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SID = database)
)
)
我想找到 xyz_write 并替换它下面的行,直到用下面的行找到空行.
I want to find xyz_write and replace the lines below it till empty line is found with below lines.
xyz_write =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostname2.com)(PORT = 80))
(CONNECT_DATA =
(FAILOVER = TRUE)
(SERVER = DEDICATED)
(SID = database)
(SERVICE = servicename)
)
)
我尝试使用 sed 命令,方法是将第一组行存储在变量中,然后将第二组行替换掉.但它没有帮助.我们如何才能做到这一点?
I tried using sed command by storing the first set of lines in a variable and replacing it will second set of lines. But it did not help. How can we achieve this ?
我尝试了以下方法:
我将第一组行分配给一个变量,如下所示.
I assigned first set of lines to a variable as below.
VARIABLE1="xyz_write =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostname.com)(PORT = 80))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SID = database)
)
)"
和
VARIABLE2="xyz_write =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostname2.com)(PORT = 80))
(CONNECT_DATA =
(FAILOVER = TRUE)
(SERVER = DEDICATED)
(SID = database)
(SERVICE = servicename)
)
)".
然后我尝试运行命令"cat source_file.txt | sed "s/$VARIABLE1/$VARIABLE2/g" > destination_file.txt".由于换行,这将不起作用.应该有办法实现这一点.
Then i tried running the command "cat source_file.txt | sed "s/$VARIABLE1/$VARIABLE2/g" > destination_file.txt". This will not work due to line breaks. There should be some way to achieve this.
推荐答案
文件 filename.txt 包含以下几行
The file filename.txt contains the lines as below
$cat 文件名.txt
$cat filename.txt
<blank line>
abc_write =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostname2.com)(PORT = 80))
(CONNECT_DATA =
(FAILOVER = TRUE)
(SERVER = DEDICATED)
(SID = database)
(SERVICE = servicename)
)
)
<blank line>
xyz_write =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostname2.com)(PORT = 80))
(CONNECT_DATA =
(FAILOVER = TRUE)
(SERVER = DEDICATED)
(SID = database)
(SERVICE = servicename)
)
)
<blank line>
我只想要一个上下文条目.对于xyz_write",我需要文件中的单个条目.在向文件中添加新条目时,检查它是否已经存在.如果已经存在xyz_write"条目,则警告用户并使用新值更新条目.因此我想更换.我以某种方式通过删除现有条目并在文件末尾附加新条目来实现这一点.
I wanted just one entry for the context. For "xyz_write" I needed single entry in the file. While adding a new entry to the file, check if it already exists. If already a entry is present for "xyz_write", warn the user and update the entry with new value. Hence I wanted to replace. Somehow I achieved this by removing the existing entry and appending the new entry at the end of file.
\#This value is a command line input.
SERVICE_NAME="xyz_write"
\#This will remove the existing entry. The command will delete the lines till empty line after the context. -i option will write to the same input file.
sed -i "/$SERVICE_NAME/,/^$/d" filename.txt
在文件末尾追加新值.
NEW_VALUE="bcd_write
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostname2.com)(PORT = 80))
(CONNECT_DATA =
(FAILOVER = TRUE)
(SERVER = DEDICATED)
(SID = database)
(SERVICE = servicename)
)
)"
echo $NEW_VALUE >>file_name.txt
这样我的目的就达到了.它实际上并没有替换多行.它通过删除现有行并附加新行来实现.希望这对某人有所帮助.
Thus my purpose is accomplished. Its not actually replacing the multiple lines. Its achieved by removing existing lines and appending the new lines. Hope this helps somebody.
谢谢,
-阿迪亚
Thanks,
-Adithya
这篇关于找到上下文后用多行替换多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!