繁体   English   中英

用文件中的多行替换一行

[英]Replace a line with multiple lines in a file

我想用多行替换文件中的一行,例如,我想替换特定的函数调用,例如,

foo(1,2)

if (a > 1) {  
    foo(1,2)  
} else {  
    bar(1,2)  
}

我怎样才能在 bash 中做到这一点?

这就是 sed s命令的目的:

shopt -s extglob

ORIG="foo(1,2)"
REP="if (a > 1) {  
      foo(1,2)  
} else {  
      bar(1,2)  
}"

REP="${REP//+(
)/\\n}"

sed "s/$ORIG/$REP/g" inputfile > outputfile

请注意,仅当您想以我在第二行中所做的格式化方式定义REP ,才需要REP="${REP//\\+( )/\\\\n}"行。 如果您刚开始在REP使用\\n\\t可能会更简单。

编辑:注意! 如果你有他们,你需要在你的 REP 中转义'\\

编辑以回应 OP 的问题

要在不创建新文件的情况下更改原始文件,请使用 sed 的--in-place标志,如下所示:

sed --in-place "s/$ORIG/$REP/g" inputfile

请小心--in-place标志。 在运行之前进行备份,因为所有更改都将是永久性的。

ORIGINAL="foo(1,2)"
REPLACEMENT="if (a > 1) {
foo(1,2)
} else {
bar(1,2)
}"

while read line; do
  if [ "$line" = "$ORIGINAL" ]; then
    echo "$REPLACEMENT"
  else
    echo "$line"
  fi
done < /path/to/your/file > /path/to/output/file 

这可能对你有用:

cat <<\! |
> a
> foo(1,2)
> b
> foo(1,2)
> c
> !
> sed '/foo(1,2)/c\
> if (a > 1) {\
>     foo(1,2)\
> } else {\
>     bar(1,2)\
> }' 
a
if (a > 1) {
    foo(1,2)
} else {
    bar(1,2)
}
b
if (a > 1) {
    foo(1,2)
} else {
    bar(1,2)
}
c

要在文件中就地替换字符串,您可以使用 ed (在问题中方便地标记)。 假设您的输入文件如下所示:

line before
foo(1,2)
line between
    foo(1,2)
line after

您可以编写一个脚本来进行替换并将其存储在一个文件中,例如script.ed

%s/\([[:blank:]]*\)foo(1,2)/\1if (a > 1) {\
\1    foo(1,2)\
\1} else {\
\1    bar(1,2)\
\1}/
w
q

请注意,这考虑了缩进; 在原始文件中的函数调用之前,每一行都带有任何空格,因此结果将如下所示:

$ ed -s infile < script.ed
$ cat infile
line before
if (a > 1) {
    foo(1,2)
} else {
    bar(1,2)
}
line between
    if (a > 1) {
        foo(1,2)
    } else {
        bar(1,2)
    }
line after

如果函数调用不是单独在一行上,而是可能由不应删除的其他字符作为前缀,则可以将其用作替换的第一行:

%s/\([[:blank:]]*\)\(.*\)foo(1,2)/\1\2if (a > 1) {\

所以这

    } something; foo(1,2)

会成为

    } something; if (a > 1) {
        foo(1,2)
    } else {
        bar(1,2)
    }

缩进仍然适当地考虑了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM