How to search and replace the specific line number string

落爺英雄遲暮 提交于 2021-02-05 08:17:45

问题


line_index="2d";

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

sed -i.bak -e $line_index $file

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

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.


回答1:


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

sed '2s/harry/potter/g'



回答2:


To restrict substitution to a line number, add it before the command:

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


来源:https://stackoverflow.com/questions/41681415/how-to-search-and-replace-the-specific-line-number-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!