开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜