How to use sed to replace a pattern in a file only in lines containing another pattern

In my specific situation, I need to scroll through several hundred files containing the variable in which the URL is stored. All URLs are different, but for this example we can assume that they all end in "com"

I need to add text right after com

Since all URLs will have different domains, I cannot use the sed command, for example:

sed -i 's/^ENVIRONMENT="someurl.com"/ENVIRONMENT="someurl.com.origin.net"/g'

Since the files contain other lines that com may contain, I cannot use something like:

sed -i 's/com/com.origin.net/g'

I need to do this search and replace only with a specific line containing the template, for example, from my example below in regex:

^ENVIRONMENT.*

sed , , sed, sed -i 's/com/com.origin.net/g'. , , bash script, , liner, sed, , , , .

Google , , :

https://unix.stackexchange.com/questions/37518/how-to-sed-only-that-lines-that-contains-given-string

. , sed , , sed.

, , .

### Example File
#
#
# someurl.com

ENVIRONMENT="someurl.com"

"ENVIRONMENT =" someurl.com "" "ENVIRONMENT =" someurl.com.orign.net "

, , sed, , script, ​​:

` f *.config; do File2BeEdited = $(cat $f | grep ^ PatternToIdentifyLine | grep -v LinesThatAreAlreadyCorrect); if [ "$ File2BeEdited"!= "], RunSedCommand; printf" \n , .\n sed $f → log.log";

+5
2

: /regex/. \0, \1 ..

sed -e '/^\(ENVIRONMENT="[^"]*\.com\)"/s//\1.origin.net"/'
+9

gvim..

: g/^ baz/s/foo/bar/g

: , baz, foo bar.

http://vim.wikia.com/wiki/Search_and_replace .

-2

All Articles