简体   繁体   中英

How to use sed against specific line in a file

There are two instances of a word music in a file.

Hello music
Goodbye music

If I were to use sed to replace an instance of music only where grep Hello is applied, what would I need to add to a sed comand?

Currently sed -i -e "s/$olmusic/$newmusic/g" file.txt replaces every instance of $oldmusic in a file but I need it to replace only the one where condition of grep is met.

我建议:

sed -i "/Hello/{s/$olmusic/$newmusic/}" file.txt

sed提议使用正则表达式作为地址

sed -i -e "/Hello/s/$olmusic/$newmusic/" file.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM