18

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

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

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

Suraj Bhatia
  • 1,233
  • 3
  • 13
  • 29
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Jan 14 '18 at 13:34
  • I would like to be on the record downvoting @jww's comment. It is a perfectly useful and valid question. – Steven Lu Aug 07 '20 at 03:00

2 Answers2

27

You can use sed addresses:

sed '19,33s/google/facebook/g' file

This will run the substitution on lines between and including 19 and 33.

The form of a sed command is as follows:

[address[,address]]function[arguments]

Where 19,33 is the addreses,
substitute is function
and global is the argument

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
4

the above answer ALMOST worked for me on Mac OSX.

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

worked perfectly without braces.

sed '19,$s/google/facebook/' file

works until the end of the file as well.

CharMstr
  • 184
  • 2
  • 8