views:

24

answers:

2

Say I have a file containing these lines:

tomatoes
bananas
tomatoes with apples
pears
pears with apples

How 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
+2  A: 

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

Benoit
A: 

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".

ghostdog74