如何在bash中的文件中的特定行的末尾附加字符串 [英] How to append a string at end of a specific line in a file in bash

查看:7
本文介绍了如何在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 和描述的模式:

Using sed and the pattern described:

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

<小时>

使用 sed 和特定的行号:

sed '2s/$/ myalias/' file

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

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