使用sed在行范围之间查找并替换文件中的文本 [英] Find and replace text in a file between range of lines using sed

查看:702
本文介绍了使用sed在行范围之间查找并替换文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大文本文件(URL.txt),我希望使用一个 sed 命令执行以下操作:

I have a big text file (URL.txt) and I wish to perform the following using a single sed command:

  1. 在行号19和第33之间找到文本"google"并将其替换为"facebook".

  1. Find and replace text 'google' with 'facebook' between line numbers 19 and 33.

在终端上显示输出而不更改原始文件.

Display the output on the terminal without altering the original file.

推荐答案

您可以为此使用SED的范围选择器:

You can use SED's range selector for that:

sed '19,33{s/google/facebook/}' file

这将在19(不包括)和33(包括)之间的行上进行替换

This will run the substitution on lines between 19 (exclusive) and 33 (inclusive)

请注意,这只会替换每行中第一次出现的google,您可以使用g-修饰符更改此行为:

Note this will only replace the first occurrence of google on each line, you can use the g-modifier to change this behavior:

s/google/facebook/g 

这篇关于使用sed在行范围之间查找并替换文件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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