Find and replace text in a file between range of l

2019-04-29 19:41发布

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.

1条回答
疯言疯语
2楼-- · 2019-04-29 20:15

You can use SED's range selector for that:

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

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

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 
查看更多
登录 后发表回答