Using sed for case insensitive search


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using sed for case insensitive search
# 1  
Old 02-17-2011
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
Code:
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 sed as below, but my version of UNIX does not support "i" option in sed.
Code:
sed 's/prodops/productionoperations/i' test_file > test_file_temp


Can anyone help me in this?

Thanks

Last edited by Franklin52; 02-18-2011 at 06:11 AM.. Reason: Please use code tags, thank you
# 2  
Old 02-17-2011
below command may help you....

Code:
 sed -i 's/[Pp][rR][oO][dD][oO][pP][sS]/productionoperations/g' test_file

# 3  
Old 02-17-2011
But my search string might have a lengthy word too. It might be cumbersome to use all possibilities in the command.

Is there any other way to do this ?

---------- Post updated at 04:58 AM ---------- Previous update was at 04:19 AM ----------

Alternately I found this perl command, that can be used to do case insensitive find and replace. But below perl command does not seem to be working if my search string has the "@" (at the rate) character in it.

perl -pe 's/prodops/productionoperations/i' test_file > test_file_temp
this works.


BUt if my search string is abc@def.com
and my replace string is xyz@jki.com, the following command is not working.


perl -pe 's/abc@def.com/xyz@jki.com/i' test_file > test_file_temp

can anyone help me how to use this perl command to replace some string with "@" character??
# 4  
Old 02-17-2011
So if you use perl, you can update the file directly by -i.tmp, test_file will be updated, and a backup file test_file.tmp will be created.
Code:
perl -i.tmp -pe 's/abc\@def.com/xyz\@jki.com/i' test_file

# 5  
Old 02-18-2011
Code:
 $ ruby -i.bakup -ne '$_.gsub!(/prodops/i,"productionoperations");print' file

# 6  
Old 02-18-2011
Hi ....
Thanks,

this command worked...
Code:
perl -i.tmp -pe 's/abc\@def.com/xyz\@jki.com/i' test_file

but can we not passs the search string and reaplce string as variables to this command??
For eg.,
if I define
Code:
SRCH_STR="abc\@def.com"
REPL_STR="xyz\@jki.com"

and if I use the command as
Code:
perl -i.tmp -pe 's/"${SRCH_STR}"/"{REPL_STR}"/i' test_file

the above does not seem to be substituting the search and replace string while executing.

Please help

Last edited by Franklin52; 02-18-2011 at 07:31 AM.. Reason: Please use code tags, thank you
# 7  
Old 02-19-2011
Code:
perl -i.tmp -pe "s/$SRCH_STR/$REPL_STR/" test_file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using awk to search case insensitive

Hello , Using the below scrip to search a string in a file , by case-insensitively Please assist on using the toupper() as getting error !. #!/usr/bin/ksh set -x curr_dir=`pwd` file_ctr=0 printf "\n Reviewing the output file from the directory: %s \n\n" $curr_dir ls -latr ... (4 Replies)
Discussion started by: Siva SQL
4 Replies

2. UNIX for Beginners Questions & Answers

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 let me know any other easier option. sed -n '/dn: MSISDN=/,/^\s*$/p' full.ldif > temp ; sed -n... (4 Replies)
Discussion started by: Kamesh G
4 Replies

3. Shell Programming and Scripting

Case insensitive file name search and replace

I am trying to find case insensitive file names and then replace that particular file with other name. if then ls | grep -i "update" | xargs -I {} mv {} LineItems.csv echo "File moved from *update*" elif then ls | grep -i "priority" | xargs -I {} mv {} ... (1 Reply)
Discussion started by: ATWC
1 Replies

4. UNIX for Dummies Questions & Answers

Command for a case insensitive search

Hi All, What is the command to search a file for a case-insensitive match 1.grep -nc text filename 2.grep -i text filename 3.grep -i filename text 4.grep -nc filename text 5.grep -c text filename Thanks for your help (1 Reply)
Discussion started by: bobby1015
1 Replies

5. UNIX for Dummies Questions & Answers

Using FIND with case insensitive search

I am using HP-Unix B.11.31. Question: How to do the case insensitive search using FIND? Example: I would like list the files with extension of *.SQL & *.sql. When I try with command find . -type f -name *.sql, it does not lists file with *.SQL. (5 Replies)
Discussion started by: Siva SQL
5 Replies

6. AIX

Case insensitive search in AIX man ?

Hello, Linux man command search is case insensitive by default, but not AIX man. How do I serch case insensitive while using AIX manual pages ? thanks Vilius (7 Replies)
Discussion started by: vilius
7 Replies

7. UNIX for Dummies Questions & Answers

more command case insensitive search ?

Hello, How do I set case insensitive search mode while the file is open with more command ? (I know -i option which could be used before opening) thanks Vilius (2 Replies)
Discussion started by: vilius
2 Replies

8. Shell Programming and Scripting

Case Insensitive search

Hey , i am trying to do a search for the certain books , and im trying to make it case insensitive. what i have come up with so far is this : Database.txt RETARDED MONKEY:RACHEAL ABRAHAML:30:30:20 GOLD:FATIN:23.20:12:3 STUPID:JERLYN:20:40:3 echo -n "Title: " read Title echo -n... (3 Replies)
Discussion started by: gregarion
3 Replies

9. Shell Programming and Scripting

case-insensitive search with AWK

Hi All, How we can perform case-insensitive search with AWK.:rolleyes: regards, Sam (11 Replies)
Discussion started by: sam25
11 Replies

10. 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
Login or Register to Ask a Question