sed-匹配后的插入行,并在插入的行的开头有一个双制表符 [英] sed - insert line after match with a double tab for the beginning of inserted line

查看:120
本文介绍了sed-匹配后的插入行,并在插入的行的开头有一个双制表符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下input.txt:

I have the following input.txt :

        "loop:\n\t" 
        "add %%g1, 1, %%g1\n\t" 

我想在第一行和第二行之间插入一行以获取:

and I want to insert a line between first and second line in order to get :

    "loop:\n\t" 
    "add %%g1, 1, %%g1\n\t" 
    "add %%g1, 1, %%g1\n\t" 

这是我尝试过的:

sed '/loop\:/a "\t \t add %%g1, 1, %%g1\\n\\t"' input.txt

但是有了这个,我得到了:

But with this, I get :

        "loop:\n\t" 
"       add %%g1, 1, %%g1\n\t" 
        "add %%g1, 1, %%g1\n\t" 

如您所见,插入行的第一引号位于行的开头,而不仅仅是add字之前.

As you can see, first quote of inserted line is at the beginning of line and not juste before add word.

如何规避此问题?

谢谢

更新1:

我意识到每行开头和第一个字符之间的空格不是制表符,而是经典空格.

I realized that spaces between the begin of each line and the first characters are not tabulations but classical spaces.

然后,我尝试使用此解决方案(来自

Then, I tried to use this solution (from this link)

awk '{print} /loop\:/{ print substr($0,1,match($0,/[^[:space:]]/)-1) "add %%g1, 1, %%g1\n\t"}' input.txt

似乎此命令行从上一行计算了正确的缩进,但不幸的是,我得到了这个提示:

It seems this command line computes the right indentation from previous line but unfortunately, I get with this :

           "loop:\n\t" 
"add %%g1, 1, %%g1\n\t" 
           "add %%g1, 1, %%g1\n\t" 

我想拥有:

           "loop:\n\t" 
           "add %%g1, 1, %%g1\n\t" 
           "add %%g1, 1, %%g1\n\t" 

您看到错误了吗?

致谢

推荐答案

sed用于在单行上进行简单替换,仅此而已.对于更有趣的事情,请使用awk代替,以提高软件的清晰度,可移植性,效率和大多数其他理想的属性:

sed is for simple substitutions on individual lines, that is all. For anything even slightly more interesting just use awk instead for clarity, portability, efficiency and most other desirable attributes of software:

$ awk '{print} /loop:/{print "\t\"add %%g1, 1, %%g1\\n\\t\""}' file
        "loop:\n\t"
        "add %%g1, 1, %%g1\n\t"
        "add %%g1, 1, %%g1\n\t"

以上只是实现了您想对sed进行的操作,但是根据您的实际需求,可能有更好的方法,例如这些中的任何一个都具有上述优点,但是在不同条件下会产生相同的输出:

The above just implements what you were trying to do with sed but there's probably a better approach depending on your real requirements, e.g. any of these has benefits over the above but produce the same output under different conditions:

$ awk '1;NR==2' file
        "loop:\n\t"
        "add %%g1, 1, %%g1\n\t"
        "add %%g1, 1, %%g1\n\t"


$ awk '{print} f{print;f=0} /loop:/{f=1}' file
        "loop:\n\t"
        "add %%g1, 1, %%g1\n\t"
        "add %%g1, 1, %%g1\n\t"

$ awk '{print} /loop:/{sub(/".*/,""); print $0 "\"add %%g1, 1, %%g1\\n\\t\""}' file
        "loop:\n\t"
        "add %%g1, 1, %%g1\n\t"
        "add %%g1, 1, %%g1\n\t"

根据您在我以前的回答下留下的评论,听起来您确实想要其中之一您不必指定缩进.

Based on the comment you left under a previous answer of mine it sounds like you do want one of those bottom so you don't have to specify the indentation.

这篇关于sed-匹配后的插入行,并在插入的行的开头有一个双制表符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆