LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-15-2010, 04:13 PM   #1
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Rep: Reputation: 16
Question Ignore case only for part of regular expression


What i want to achieve is, if I have following in my file:

x y
X z

I should be able to find the pair, x Y and replace it with x a.

So, basically I want to search for case sensitive 'x' but search and replace case insensitive 'y'.

How should I do it ??
Can this be achieved with 'sed' command ??
 
Old 02-15-2010, 05:40 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,808

Rep: Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604
Depending on the flavor of regex - (?i) to turn INsensitivity on and (?-i) to turn it sensitive again.
 
Old 02-16-2010, 12:25 AM   #3
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 16
Does this works for Shell script ??

echo "fooFoOBAR" | egrep "(?i)foo(?-i)"

returned nothing
 
Old 02-16-2010, 01:18 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I've never seen the construct that Alucar* shows--perhaps he can give us a reference.

The common way to specify a character which can be either case is the "character class"---eg "[aA]" matches "a" or "A".

Various utilities have flags for ignoring case--eg "grep -i"
 
Old 02-16-2010, 04:55 AM   #5
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 16
Finally got it,
Single sed command and it's done.

sed -i /regex/s/regex/replacement text/i <file>

Example:

sed -i '/^[ \t]*x [ \t]*/s/[ \t]* y[ \t]*$/ abc/gi' <file>

This is working for me.

Suggestions/comments are most welcome
 
Old 02-16-2010, 08:23 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by luvshines View Post
Finally got it,
Single sed command and it's done.

sed -i /regex/s/regex/replacement text/i <file>

Example:

sed -i '/^[ \t]*x [ \t]*/s/[ \t]* y[ \t]*$/ abc/gi' <file>

This is working for me.

Suggestions/comments are most welcome
This may not be doing what you think....

First, it appears to be looking for any number of "tab" or space characters in several places. Is that relevant to your problem?

Second the "I" or "i" at the end of the s command makes it case-INsensistive, but just for the s command. This is a Gnu extension, which I found here:
http://www.gnu.org/software/sed/manual/sed.html

Regardless of the spaces and tabs, it's finding lower case "x", and then doing the substitution on "y" or "Y".
 
Old 02-25-2010, 12:39 PM   #7
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by pixellany View Post
This may not be doing what you think....

First, it appears to be looking for any number of "tab" or space characters in several places. Is that relevant to your problem?

Second the "I" or "i" at the end of the s command makes it case-INsensistive, but just for the s command. This is a Gnu extension, which I found here:
http://www.gnu.org/software/sed/manual/sed.html

Regardless of the spaces and tabs, it's finding lower case "x", and then doing the substitution on "y" or "Y".
The "tabs" and "spaces" are very much relevant and I think it's working exactly as expected.

The problem I had was regarding, yp.conf

domain <domainname> server <servername>

entries

The domain in NIS is case sensitive but the servername is case insensitive.
I had to write a script to find and modify server name for a particular domain
So, I had to look for case-sensitive <domainname> and then replace all occurrences of <servername> with the new server.
The x in my sed command denotes the domainname and y/Y is for server
 
Old 02-25-2010, 12:53 PM   #8
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,808

Rep: Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604
Quote:
Originally Posted by pixellany View Post
I've never seen the construct that Alucar* shows--perhaps he can give us a reference.
Maybe it's specific to Unreal IRCd?
 
Old 02-25-2010, 10:39 PM   #9
mattseanbachman
Member
 
Registered: Feb 2010
Posts: 40

Rep: Reputation: 15
I think i know how to do this with the -P switch with grep (P standing for Perl syntax). For that you should be able to specify things like [a-z]\x20[a-zA-z] THe first thing in braces will just match lowercase a-z, the middle is a space, and the next matches a character that's either upper or lower case.

I hope this helps you out. This is all assuming you're using grep I guess, I'm uncertain what tool you have so I can't give more help unless I know that.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] what does this regular expression do? smeezekitty Programming 10 10-07-2009 07:12 PM
regular expression Ammad Linux - General 5 08-01-2008 08:41 AM
regular expression (.*?) uttam_h Programming 6 05-30-2008 06:45 PM
Need help with Regular Expression subaruwrx Linux - Newbie 6 09-04-2004 08:48 PM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 11:08 AM


All times are GMT -5. The time now is 10:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration