开发者

sed: How do I replace all lines containing a certain string?

开发者 https://www.devze.com 2023-01-19 23:34 出处:网络
Say I have a file containing these lines: tomatoes bananas tomatoes with apples pears pears with apples Ho开发者_JAVA技巧w do I replace every line containing the word \"apples\" with just \"oranges

Say I have a file containing these lines:

tomatoes
bananas
tomatoes with apples
pears
pears with apples

Ho开发者_JAVA技巧w do I replace every line containing the word "apples" with just "oranges"? This is what I want to end up with:

tomatoes
bananas
oranges
pears
oranges


Use sed 's/.*apples.*/oranges/' ?


you can use awk

$ awk '/apples/{$0="orange"}1' file
tomatoes
bananas
orange
pears
orange

this says to search for apples, then change the whole record to "orange".

0

精彩评论

暂无评论...
验证码 换一张
取 消