如何在一个特定的行结束在bash一个文件追加一个字符串 [英] How to append a string at end of a specific line in a file in bash

查看:100
本文介绍了如何在一个特定的行结束在bash一个文件追加一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲别名附加到我的主机文件的特定行的结尾。例如:

I want to append an alias to the end of a certain line of my hosts file. For example

192.168.1.1    www.address1.com
192.168.1.2    www.address2.com
192.168.1.3    www.address3.com

我想它看起来像

192.168.1.1    www.address1.com
192.168.1.2    www.address2.com myalias
192.168.1.3    www.address3.com

我要找到包含19.2.68.1.2线,并在它的结束追加myalias。线不一定是在文件中的第二行像我在这里示出。它可以在任何地方。

I want to find the line that contains 19.2.68.1.2 and append myalias at the end of it. The line is not necessarily the second line in the file like I've shown here. It could be anywhere.

推荐答案

使用 SED 和描述的模式:

sed '/192.168.1.2/s/$/ myalias/' file


使用 SED 和特定的行号:

sed '2s/$/ myalias/' file

这篇关于如何在一个特定的行结束在bash一个文件追加一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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