Visit Our UNIX and Linux User Community


Making SED case insensitive


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Making SED case insensitive
# 1  
Old 05-03-2018
Making SED case insensitive

Dears,

In the below string, please let me know how to make the sed search case-incensitive. I have more such lines in my script instead of [Aa] let me know any other easier option.


Code:
sed -n '/dn: MSISDN=/,/^\s*$/p' full.ldif > temp ; sed -n '/,dc=msisdn,ou=identities,'"$LDAP_ROOT_DN"'/,/^\s*$/p' temp > alias_MSISDN_PL.ldif ; rm temp

regards,
Kamesh G
# 2  
Old 05-03-2018
If you're using GNU sed, you can add the I modifier to the end.

e.g.
Code:
$ echo HELLO | sed "s/hello/hi/I"
hi

Otherwise, you can pipe the input through tr first, for example.
# 3  
Old 05-03-2018
The I modifier also works with addresses:
Code:
echo HELLO | sed "/hello/I s//hi/"

Code:
sed -n '/dn: MSISDN=/I,/^\s*$/p'

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 05-03-2018
Quote:
Originally Posted by Kamesh G
In the below string, please let me know how to make the sed search case-incensitive. I have more such lines in my script instead of [Aa] let me know any other easier option.
I can't offer you a (single) command but a procedure how to do it - even with a standards-conforming (non-GNU) sed:

- First, save your pattern space to the hold space.

- Next, you use the y/.../.../-command to transliterate to lower

- Finally you do your matching and get from the hold space the saved copy of the pattern space on which you perform whatever action you wanted to perform.

Here is an (very simple) example: we search (case-insensitively) for "hello" at the beginning of the line and add a equal-sign in front of it. The comments are NOT part of the script:

Code:
echo 'Hello
foo
hElLo
helLO' |\
sed 'h                 # pattern to hold space
      y/HELO/helo/     # we need only to convert what we match
      /^hello/ {
            g          # get the original line from hold space
            s/^/=/
            b end
      }
      /^hello/ ! {
            g
      }
      :end'

I hope this helps.

bakunin

Last edited by bakunin; 05-03-2018 at 11:40 PM..
These 2 Users Gave Thanks to bakunin For This Post:
# 5  
Old 05-04-2018
Good idea!
Can be optimized: after the b end one is automatically in an "else branch" and does not need to repeat and negate the previous condition.
Code:
echo 'Hello
foo
hElLo
helLO' | sed '
  h
  y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
  /^hello/{
    g
    s/^/=/
    b end
 }
 g
 :end
'

This time I have done a complete ASCII lowercase conversion.
This works with all sed versions.
Of course the GNU I modifier is much simpler.

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use case insensitive variable in ksh shell scripting using sed or awk

I am using a variable called $variable in a pattern search to print from a starting variable to a constant value. the variable search should be case in sensitive. i tired using Ip at the end in the below command. but in ksh it is not working. sed -n "/$variable/,/constant/p" file i also... (11 Replies)
Discussion started by: johnjs
11 Replies

2. Shell Programming and Scripting

making sed command case insensitive

i have something like this in a file cat onlytables.sql create table NextID ( id int auto_increment, zoneID int, entityName varchar(64), nextID int, lastModified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, primary... (6 Replies)
Discussion started by: vivek d r
6 Replies

3. Shell Programming and Scripting

Making case insensitive perl statement

cat > tble blah blah blah sdfsdf dsdf sdf .d.f ..df .. sdf.. create table NextID ( id int auto_increment, zoneID int, entityName varchar(64), nextID int, lastModified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, primary... (6 Replies)
Discussion started by: vivek d r
6 Replies

4. Shell Programming and Scripting

Making case insensitive in awk

here is a statement awk '/CREATE PROCEDURE/,/elimiter/' "$file1" > onlyproc1.sql which mean cut from create procedure to Delimiter or delimiter and paste it in onlyproc1.sql... my query is how to make this case insensitive.. that is i want the above code to work whther it is Delimiter or... (26 Replies)
Discussion started by: vivek d r
26 Replies

5. Shell Programming and Scripting

Making expr match case insensitive...

here is the condition i am using ] how to make this case insesntive... that is it should work with smaller case of "index" too... (11 Replies)
Discussion started by: vivek d r
11 Replies

6. UNIX for Dummies Questions & Answers

Using sed for case insensitive search

Hi, I have a file named "test_file" that has the below content. It has words in upper/lower cases PRODOPS prodOPS ProdOps PRODops escalate Shell My requirement is to replace all the "prodops" (what ever case it may be) with "productionoperations". I tried using the "i" option with... (7 Replies)
Discussion started by: sbhuvana20
7 Replies

7. Shell Programming and Scripting

How to make sed case insensitive

I need to remove a pattern say, ABCD whether it is in uppercase or lowercase from a string. How to do it using SED? for example ABCDEF should output to EF abcdEF should also output to EF (2 Replies)
Discussion started by: vickylife
2 Replies

8. Shell Programming and Scripting

case insensitive

hi everyone, I need to do the following thing in a case insesitive mode sed 's/work/job/g' filename since work could appear in different form as Work WORK WorK wORK,.... I was wondering if i could do a case insensitive search of a word. thanks in advance, :) (4 Replies)
Discussion started by: ROOZ
4 Replies

9. Shell Programming and Scripting

awk case-insensitive

can I tell awk to be case insensitive for one operation without setting the ignorecase value ? thanks, Steffen (7 Replies)
Discussion started by: forever_49ers
7 Replies

10. UNIX for Dummies Questions & Answers

case insensitive locate

How can I do a case insensitive locate? (3 Replies)
Discussion started by: davis.ml
3 Replies

Featured Tech Videos