sed: replace exact string (upper and lower cases) (524 Views)
Reply
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 1 of 3 (524 Views)
Accepted Solution

sed: replace exact string (upper and lower cases)

hello,

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
Dennis Handly
Posts: 25,290
Registered: ‎03-06-2006
Message 2 of 3 (469 Views)

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/' \

   init.ora

Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 3 of 3 (419 Views)

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_ACT="INUA"
STRING_OLD="INTE"
CHANGE_FILE="init.ora"

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

P_STRING_ACT="${STRING_ACT}" \
P_STRING_OLD="${STRING_OLD}" \
P_STRING_OLD_LOW="${STRING_OLD_LOW}" \
P_STRING_ACT_LOW="${STRING_ACT_LOW}" \
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.

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.