BladeSystem Forums Have Moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

sed: replace exact string (upper and lower cases)

Go to Solution
Valued Contributor

sed: replace exact string (upper and lower cases)


i have a file ( parameter file of a database. filename: init.ora). the string length of the database sid are 4 characters.

database sid name is : INTE . i want to replace the sid name to a new sid name. i have combinations like:


- INTE_inte or INTE or /INTE/ # comment

- but there are some strings like: query_rewrite_integrity ... this string also include "inte" , but this string i don't want to replace !


so i search for the string "INTE" or "inte"

grep -E "INTE[^A-Z]|INTE$|inte[^a-z]|inte$" init.ora

 old replacement was ( not exact)


sed -e "s|INTE|AAAA|g" -e "s|inte|aaaa|g"

 but this statement works with Linux "sed" and not with HP-UX "sed" and didn't the right output:

sed -e "s/INTE\([^A-Z]\|$\)/AAAA\1/" -e "s/inte\([^a-z]\|$|\)/aaaa\1/" init.ora


are there any alternatives ? an example file is in the attachments, but you have to rename from init_ora.txt to init.ora


kind regards

Acclaimed Contributor

Re: sed: replace exact string (upper and lower cases)

>are there any alternatives?


You're trying to use sed with EREs.  Instead repeat the script twice for each:

sed -e 's/INTE\([^A-Z]\)/AAAA\1/g' \
        -e 's/INTE$/AAAA/' \
        -e 's/inte\([^a-z]\)/aaaa\1/g' \
        -e 's/inte$/aaaa/' \


Valued Contributor

Re: sed: replace exact string (upper and lower cases)



in the meantime i got support of a perl programer


perl -p -e 'while (/INTE/gi) { if (substr($_,$+[0]) !~/=/ && substr($_,0,$-[0]) !~/#/) { s/INTE/INUA/; s/inte/inua/; } }' init.ora


the usage in a shell script will be ( i didn't a better solution) :


STRING_OLD_LOW=$(echo ${STRING_OLD} | tr '[:upper:]' '[:lower:]')
STRING_ACT_LOW=$(echo ${STRING_ACT} | tr '[:upper:]' '[:lower:]')

perl -i -p -e 'while (/$ENV{P_STRING_OLD}/gi) { if (substr($_,$+[0]) !~/=/ && substr($_,0,$-[0]) !~/#/) { s/$ENV{P_STRING_OLD}/$ENV{P_STRING_ACT}/; s/$ENV{P_STRING_OLD_LOW}/$ENV{P_STRING_ACT_LOW}/; } }' ${CHANGE_FILE


i mean, the "sed" is easier to read.