如何搜索和替换特定的行号字符串 [英] How to search and replace the specific line number string

查看:43
本文介绍了如何搜索和替换特定的行号字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

line_index="2d";

file="./Desktop/books.sh";

sed -i.bak -e $line_index $file

将删除 $line_index 指向的整行

Will delete the entire line that $line_index is pointing to

sed -i "s/harry/potter/g" $file

将搜索哈利并替换为波特

will search for harry and replace with potter

无论如何我可以将这两个语句结合起来,这样它只会替换harry的特定行号而不是文件中的所有harry.

Is there anyway that i can combine both statement so it will only replace the specific line number of harry instead of all the harry in the file.

推荐答案

当然,地址"和命令"可以自由组合:

Sure, "addresses" and "commands" can be freely combined:

sed '2s/harry/potter/g'

这篇关于如何搜索和替换特定的行号字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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