如何在特定文本后添加带有sed/awk的行 [英] how to append a line with sed/awk after specific text

查看:97
本文介绍了如何在特定文本后添加带有sed/awk的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用sed或awk翻译此输入文件:

i would like to translate this input file using sed or awk:

input
1 AA
3 BB
5 CC

output
1 AA
3 BB
3 GG
5 CC

我在此站点sed -i '/^BB:/ s/$/ GG/'文件中找到的最接近的语法,但确实存在3 BB GG.我需要的是类似于vi yank,paste&正则表达式替换. 可以用sed或awk完成吗?谢谢 兰德

the closest syntax I found on this site sed -i '/^BB:/ s/$/ GG/' file but it does 3 BB GG. What I need is similar to a vi yank, paste & regex replace. can this be done with sed or awk? thanks Rand

推荐答案

awk是一个不错的选择:

awk is a fine choice for this:

awk '{print $0} $2=="BB"{print $1,"GG"}' yourfile.txt

这将打印行{print $0}.然后,如果该行的第二个字段等于"BB",它将打印该行的第一个字段(数字)和文本"GG".

That will print the line {print $0}. And then if the second field in the line is equal to "BB", it will print the first field in the line (the number) and the text "GG".

使用示例:

>echo "1 AA\n3 BB\n4 RR" | awk '{print $0} $2=="BB"{print $1,"GG"}'
1 AA
3 BB
3 GG
4 RR

这篇关于如何在特定文本后添加带有sed/awk的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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