Sed命令用于在特定文本下方添加。

huangapple go评论55阅读模式
英文:

Sed command to append below the specific text

问题

I've a scenario where I want to add some content below specific line as shown below.

func (v version) string(){
	vers := [...]string{
		"4",
		"5",
       //"6",         //this should be added
		"vE",
	}

Above is my code block and I want to add "6" below "5" using the sed operation.
I've seen multiple articles and posts but couldn't get the correct answer.

What I've tried is:

current_number = '"5",'
next_number = '"6",'
os.system("sed -i /"+current_number+"/a"+next_number+" sample.txt")

Edit 1: After @steffen's answer, I get the following error, image attached. Sed命令用于在特定文本下方添加。

英文:

I've a scenario where I want to add some content below specific line as shown below.

func (v version) string(){
	vers := [...]string{
		"4",
		"5",
       //"6",         //this should be added
		"vE",
	}

Above is my code block and I want to add "6", below "5" using sed operation.
I've seen multiple articles and post but couldn't get the correct answer.

What I've tried is

    current_number = '"5",'
    next_number = '"6",'    
    os.system("sed -i /"+current_number+"/a"+next_number+" sample.txt")

Edit 1 : After @steffen's answer I get below error, image attached.Sed命令用于在特定文本下方添加。

答案1

得分: 1

以下是翻译好的部分:

  • "It works:" 可以工作:
  • "But I think you need to add single quotes. I think your command reads:" 但我认为你需要添加单引号。我认为你的命令读取:
  • "And that's equivalent to:" 这等同于:
  • "Here's how to use variables:" 这是如何使用变量的方法:
  • "The latter uses an idiomatic format, ' for all the literals and " for variables." 后者使用一种习惯性的格式,对于所有字面量使用 ',对于变量使用 "。
英文:

It works:

$ sed '/"5",/a"6",' file
func (v version) string(){
    vers := [...]string{
        "4",
        "5",
"6",
        "vE",
    }

But I think you need to add single quotes. I think your command reads:

sed -i /"5",/a"6", sample.txt

And that's equivalent to:

sed -i /5,/a6, sample.txt

Here's how to use variables:

PATTERN='"5",'
NEWLINE='"6",'
sed "/$PATTERN/a$NEWLINE" file
sed '/'"$PATTERN"'/a'"$NEWLINE" file # alternatively

The latter uses an idiomatic format, ' for all the literals and " for variables.

答案2

得分: 0

以下是翻译好的内容:

给定这些变量:

$ current_number='"5"'
$ next_number='"6"'

如果您真的想要使用sed,您可以执行以下操作以保留新行的缩进:

$ sed -n 'p; s/'"$current_number"'/'"$next_number"'/p' file
func (v version) string(){
    vers := [...]string{
        "4",
        "5",
        "6",
        "vE",
    }

但个人建议出于未来更新、维护等方面的便利性使用awk:

$ awk -v old="$current_number" -v new="$next_number" '{print} sub(old,new)' file
func (v version) string(){
    vers := [...]string{
        "4",
        "5",
        "6",
        "vE",
    }

使用GNU awk时,使用awk -i inplace ...就像您使用GNU sed时的sed -i ...来更新输入文件。

英文:

Given these variables:

$ current_number='"5"'
$ next_number='"6"'

If you really want to use sed you could do the following to retain the indentation for the new line:

$ sed -n 'p; s/'"$current_number"'/'"$next_number"'/p' file
func (v version) string(){
    vers := [...]string{
        "4",
        "5",
        "6",
        "vE",
    }

but personally I'd use awk for ease of future updates, maintenance, etc:

$ awk -v old="$current_number" -v new="$next_number" '{print} sub(old,new)' file
func (v version) string(){
    vers := [...]string{
        "4",
        "5",
        "6",
        "vE",
    }

Use awk -i inplace ... with GNU awk like you're using sed -i ... with GNU sed to update the input file.

huangapple
  • 本文由 发表于 2023年6月12日 16:04:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定