LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   SED - case insensitive pattern restriction (https://www.linuxquestions.org/questions/linux-general-1/sed-case-insensitive-pattern-restriction-743040/)

filip 07-27-2009 04:38 AM

SED - case insensitive pattern restriction
 
Hi,
I have a lot of Oracle DB package bodies in files and need to replace the names of internal called procedures/functions.
A small example:
suppose we want to change InsertCustomer to InsCust in a procedure like this:

PROCEDURE InsertCustomers () AS
BEGIN
SELECT InsertCustomer() FROM DUAL; -- this should be replaced
...
SELECT someOtherPackage.InsertCustomer() FROM DUAL -- this not
END InsertCustomers;

I need a restriction for sed, which excludes the lines 1,5 and 6 but in case insensitive mode (END can also be end or End, ...). There's an option /i in sed for the case insensitive mode, but that only applies for replacement and not for the restriction pattern.

I use following command, but that excludes only lines with PROCEDURE, FUNCTION, END in upper case and 'anything.InsertCustomer':

sed '/^\(PROCEDURE\|FUNCTION\|END\|.*\+\.InsertCustomer\)/ !s/InsertCustomer/InsCust/i'

I also need to have the ability to exclude lines like this:
anything.insertcustomer
anything.INSERTCUSTOMER
End
procedure
...

Thank you for any help.
Filip

David the H. 07-27-2009 09:38 AM

According to this, gnu sed has an 'I' flag for case-insensitivity that can be used after regex expressions. I've tried a couple of simple tests and it seems to work both in the matching and in the replacement expressions.

filip 07-28-2009 03:23 AM

Hi David,
thank you for your answer. You are absolutely right about the I flag, but as you said it works for matching and replacement, but not for the restriction regex.

Take a sed command like this:

sed '/restriction_regex/ s/match_regex/replace_regex/I'

The I flag will work on match_regex and replace_regex, but the restriction_regex will not be executed in case insensitive mode.

David the H. 07-28-2009 08:08 AM

It's certainly working for me.
Code:

$ testvar='1. FOOBAR
2. foobar
3. FooBar
4. barbum
5. BARBUM'


$ echo "$testvar" |sed -n '/foo/I s/bar/bum/Ip'
1. FOObum
2. foobum
3. Foobum

$ echo "$testvar" |sed -n '/BUM/I s/bar/bum/Ip'
4. bumbum
5. bumBUM



All times are GMT -5. The time now is 07:24 AM.