gpt4 book ai didi

Bash sed 替换直到行尾

转载 作者:行者123 更新时间:2023-12-02 15:51:20 24 4
gpt4 key购买 nike

我有以下输入文件:

this is text
identifier {
item /directory/file_1234
some more text
item /directory/file_1234 other line text
identifier {
item /directory/98_76file
other line

我想用 /directory/file_ABC 替换 identifier { 行之后的字符串 item/directory/*

我试过了

cat input | sed ':a;N;$!ba;s/    identifier {\n        item \/directory\/.*$/    identifier {\n        newitem \/directory2\/file_ABC/g'

但它会在第一次出现后删除这些行

this is text
identifier {
newitem /directory2/file_ABC

最佳答案

假设你想替换这两个事件,你可以使用这个 sed

$ sed '/identifier {/{N;s~\(.*\n[^[:alpha:]]*\).*~\1newitem /directory/file_ABC~}' input_file
this is text
identifier {
newitem /directory/file_ABC
some more text
item /directory/file_1234 other line text
identifier {
newitem /directory/file_ABC
other line

关于Bash sed 替换直到行尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72470462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com