0

I am new to shell scripting and have a request to modify multiple files. I have the input as below

#this is line1 for [email protected]     
test line 2  
test line 3  
this is line4 for [email protected]  
test line 5  
this is line6 for [email protected]

and need output like

#this is line1 for [email protected]   
test line 2  
test line 3   
##this is line4 for [email protected]  
this is line4 for [email protected]  
test line 5   
##this is line6 for [email protected]  
this is line6 for [email protected]

I tried the below command to do this changes and it changes only abc to xyz and other are not getting changed

sed '/^[^#].*@abc.com/ {h; s/^/##/; p; g; s/abc.com/xyz.com/;}'

Please help me to modify the script case-insentitive

4
  • Glenn... Please help Commented Jan 21, 2016 at 20:19
  • what do the bold lines mean? Commented Jan 21, 2016 at 20:24
  • So replacement is not static xyz.com and it has to match case also like Abc.COM => Xyz.COM? Commented Jan 21, 2016 at 21:19
  • Not necessarily. Abc.com can become xyz.com, and ABC.com can become xyz.com. The only concern is that the read from the source should be case insensitive. But it would be better if the source and target matches if there is an option Commented Jan 22, 2016 at 14:58

1 Answer 1

1

You're asking how to do case insensitive matching and replacement.

There's no good and portable way of doing this with sed. You can instead use e.g. [Aa] to match either A or a:

sed '/^[^#].*@[Aa][Bb][Cc]\.[Cc][Oo][Mm]/ {
        h; s/^/##/; p; g; s/[Aa][Bb][Cc]\.[Cc][Oo][Mm]/xyz.com/;
     }'

You can rewrite this to a single substitution to save some bytes:

sed 's/^\([^#].*\)[Aa][Bb][Cc]\.[Cc][Oo][Mm]\(.*\)/##&\
\1xyz.com\2/'

However, if you're using GNU sed (and not e.g. OSX sed), then you can use the I flag to s:

sed 's/^\([^#].*\)abc\.com\(.*\)/##&\n\1xyz.com\2/I'  # GNU only
Sign up to request clarification or add additional context in comments.

1 Comment

I am using Hp-ux and I flag is not working, may be i have try the first option

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.