Ignoring case in sed search


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ignoring case in sed search
# 1  
Old 03-12-2010
Ignoring case in sed search

I am getting a parameter from a user and I need to use it to search and return the matching line numbers in a file. I am using this code:

Code:
recordNumber="$(sed -n '/'"$entry"'/{
=
d
}' unixdb1.txt)"

where $entry is the passed search parameter. The problem is I need to ignore the case. I've seen some searches where they say you can use the i or I option but it doesn't work for me. Is there anyway to do this or can it be done a different way? Thanks.
# 2  
Old 03-12-2010
Code:
awk '/[Ee][Nn][Tt][Rr][Yy]/ {print NR}' infile

# 3  
Old 03-12-2010
Hi, snag49ers:

If you are using GNU SED and portability isn't a concern, I believe you can follow an address with an I flag. For example, to print only lines that case-insensitively match "searchterm":
Code:
sed -n '/searchterm/I p'

That said, I think this is a poor solution to this problem. If the user-provided parameter contains characters which have a special meaning to sed at that location in the command, your code will make a mess of things. I would suggest the following:
Code:
grep -Fin "$entry" unixdb1.txt | cut -d: -f1

Whatever the value of $entry may be, it will be matched properly and there's no chance of regexp metacharacters messing things up.

Cheers,
Alister
# 4  
Old 03-12-2010
Thanks Alister. I went ahead and used the grep and cut. I knew how to do that one but I just wondered if you could use sed. Portability is definitely an issue and I don't have GNU sed so I can't use the I option. Thanks.


Bill
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicate lines after ignoring case and spaces between

Oracle Linux 6.5 $ cat someStrings.txt GRANT select on MANHPRD.S_PROD_INT TO OR_PHIL; GRANT select on MANHPRD.S_PROD_INT TO OR_PHIL; GRANT select on SCOTT.emp to JOHN; grant select on scott.emp to john; grant select on scott.dept to hr;If you ignore the case and the empty space between the... (6 Replies)
Discussion started by: kraljic
6 Replies

2. Shell Programming and Scripting

awk search for max and min while ignoring special character

I am trying to get a simple min/max script to work with the below input. Note the special character (">") within it. Script awk 'BEGIN{max=0}{if(($1)>max) max=($1)}END {print max}' awk 'BEGIN{min=0}{if(($2)<min) min=($2)}END {print min}' Input -122.2840 42.0009 -119.9950 ... (7 Replies)
Discussion started by: ncwxpanther
7 Replies

3. Shell Programming and Scripting

ignoring case

in if clause , how 1 can ignore case (i.e. small or capital) ny suggestions? (2 Replies)
Discussion started by: Gl@)!aTor
2 Replies

4. Shell Programming and Scripting

Search by patterns case

42 network read failed sv1 sv23 sv4 sv11 sv23 sv5 sv 7 48 client hostname could not be found sv21 sv78 sv19 sv22 sv111 sv203 sv5 sv 33 49 client did not start sv1 sv21 54 timed out connecting to client sv2 sv4 sv12 above is my file , I'd like to use a script to list all name... (5 Replies)
Discussion started by: Sara_84
5 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Ignoring file name case and decrypting it.

Dear Friends, I want to decrypt 2 different file types in a folder (ZIP files and GPG files). Each file type need different decryption syntex. Hence, the script should identify file type and should act accordingly ignoring file name case i.e. upper or lower case. Also, the extention can be... (6 Replies)
Discussion started by: anushree.a
6 Replies

8. Shell Programming and Scripting

sed ignoring case for search but respecting case for subtitute

Hi I want to make string substitution ignoring case for search but respecting case for subtitute. Ex changing all occurences of "original" in a file to "substitute": original becomes substitute Origninal becomes Substitute ORIGINAL becomes SUBSTITUTE I know this a little special but it's not... (1 Reply)
Discussion started by: kmchen
1 Replies

9. Shell Programming and Scripting

Ignoring newlines in my search

I have a file that has lines that are deliminated with '^A', but some of the lines go for a few lines and I need those lines to be appended into one line. All of the lines start with 'low debug' and end with ' " 0 '. How can I read each line from start to finish without some of the data... (7 Replies)
Discussion started by: ndedhia1
7 Replies

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