LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-14-2021, 08:46 PM   #1
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Rep: Reputation: 15
Have sed case sensitive only some part


How do we in searching by regex e.g.
Code:
sed -Ee '/^\s*[0-9]+.+\*FOOBAR=.+\bfoo\b.+\bbar/p'  F
How do we find exact FOOBAR= and any letter case foo and any letter case bar
Thanks much
 
Old 10-14-2021, 09:26 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
I don't think sed is advanced enough to do that, at least not in a tidy manner.

Code:
sed -n -Ee '/^\s*[0-9]+.+\*FOOBAR=.+\b[Ff][Oo][Oo]\b.+\b[Bb][Aa][Rr]/p'  F
However, you can do it easily with Perl or PCRE:

Code:
perl -n -e '/^\s*[0-9]+.+\*FOOBAR=.+(?i)\bfoo\b.+\bbar/ && print' F

grep -h -P -e '^\s*[0-9]+.+\*FOOBAR=.+(?i)\bfoo\b.+\bbar' F
The (?i) turns on case-insensitivity. Leaving off a matching (?-i) means it applies through to the end of the pattern.

See "man perlre" and scroll down to the section on Extended Patterns and the paragraphs with the heading "(?adlupimnsx-imnsx)" and "(?^alupimnsx)"

Last edited by Turbocapitalist; 10-15-2021 at 01:03 AM. Reason: correction of -n with sed, -h with grep
 
Old 10-15-2021, 12:26 AM   #3
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
Rather than
Code:
sed -Ee '/REGEXP/p'  F
perhaps you want
Code:
sed -En '/REGEXP/p'  F
 
Old 10-15-2021, 02:32 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Would this be OK?
Code:
sed -En '/^\s*[0-9]+.+\*FOOBAR=/s/=.+\bfoo\b.+\bbar/&/Ip'  F
 
Old 10-15-2021, 03:08 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by shruggy View Post
Would this be OK?
Code:
sed -En '/^\s*[0-9]+.+\*FOOBAR=/s/=.+\bfoo\b.+\bbar/&/Ip'  F
Just curious, where is this I flag documented?
In the official documentation I could only find:
Code:
I
i

    The I modifier to regular-expression matching is a GNU extension which makes sed match regexp in a case-insensitive manner.
But probably the upcase and lowercase I are different (and somewhere explained).
 
Old 10-15-2021, 03:38 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
No, in my experience they're identical (as a flag to the substitute command). The upper-case variant first appeared in GNU sed 3.01 as a modifier to RE address:
Quote:
Originally Posted by NEWS
Sed 3.01
[...]
  • One new feature has been added: regular expressions may be followed with an "I" directive ("i" was taken [the "i"nsert command]) to indicate that the regexp should be matched in a case-insensitive manner.
Quote:
Originally Posted by ChangeLog
Thu Aug 14 11:30:04 PDT 1997 Ken Pizzini <ken@gnu.org>
  • compile.c: added case-insensitive modifier ('I') to address and s/// regexps. The s/// case also accepts the more popular 'i' modifier. (The address regexp cannot use 'i' as a modifier, as that conflicts with the use of the 'i'nsert command.)
So the example above could be simplified as
Code:
sed -En '/^\s*[0-9]+.+\*FOOBAR=/{/=.+\bfoo\b.+\bbar/Ip}'  F
 
2 members found this post helpful.
  


Reply

Tags
case-insensitive, sed



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
Help with sed and awk to change L-case letters to U-case for specific lines in a file rootaccess Linux - General 12 05-21-2012 02:50 PM
Copying files from case-sensitive Linux to case-insensitive Windows via CIFS? SlowCoder Linux - General 4 05-07-2008 07:03 PM
case-sensitive issue david_wliu Linux - General 4 03-06-2003 10:46 PM
can vfat mounted dir be case sensitive? oldbee Linux - Software 7 01-08-2002 02:12 PM
makeing Find command not case sensitive? -ilname isnt working. systemgsr Linux - General 2 12-25-2001 12:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:26 AM.

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
Open Source Consulting | Domain Registration