匹配模式后添加行 [英] Add line after matching a pattern

查看:36
本文介绍了匹配模式后添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件 test ,具有以下值

I have a file say test with following values

Linux
Solaris
Fedora
Ubuntu
AIX
HPUX

如何在匹配AIX的行后添加带有系统主机名的行?如果我这样做

How to add a line with system hostname after the line matching AIX? If I do

echo `hostname` >> test

系统主机名在HPUX之后排在最后.

system hostname comes at the last after HPUX.

推荐答案

使用sed:

sed "s/^AIX$/& $(hostname)/" file

如果该行可以包含 AIX :

sed "s/AIX.*/& $(hostname)/" file

要在匹配的行后附加主机名,请尝试 a (附加)命令:

To append the hostname after the matching line, try the a(append) command:

sed "/^AIX$/a $(hostname)" file

这篇关于匹配模式后添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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