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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use case insensitive variable in ksh shell scripting using sed or awk
# 1  
Old 06-18-2012
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.

Code:
sed -n "/$variable/,/constant/p" file

i also tried the below awk commad using toupper() function.. it works only for only one case...

Last edited by Scrutinizer; 06-18-2012 at 04:22 AM.. Reason: code tags
# 2  
Old 06-18-2012
Code:
perl -ne '(/'$variable'/../constant/) && print' file

# 3  
Old 06-18-2012
The -I flag is only in GNU sed and only with the substitute command, not with a range match.. Try:
Code:
awk 'tolower($0)~tolower(s),/constant/' s="$variable" file

note: if $variable contains regex expressions then this may not work as expected..
# 4  
Old 06-18-2012
Both the solutions are not working..

I tried
Code:
awk 'tolower($0) ~ /'$variable'/,/endpattern' filename

but is working only if we give the input variable in lowercase and the actual pattern in file is uppercase. i want to search the file for both uppercase and lowercase irrespective of the input case we are giving.

Last edited by Scrutinizer; 06-18-2012 at 09:15 AM.. Reason: code tags
# 5  
Old 06-18-2012
Could you post a sample of your input, desired output and the content of $variable and your OS and version?
# 6  
Old 06-18-2012
Can you please let me know if this works:

Code:
sed -n "/"$(awk 'index(toupper($0),toupper("'$variable'"))>0{print $0; exit}' file1)"/,/constant/p" file1

# 7  
Old 06-18-2012
There are some questions i have, because your object wasn't completely clear to me:

1. You want to search case-insensitive, but what about the output? Will it matter in which case it is or should the original capitalization be preserved?

If you don't care about the capitalization it is relatively easy: transform your search string to lower-case first (you can do this via a mechanism built-in to the shell), then, prior to searching, modify the patternspace to contain only lower-case before searching. Notice that the following scripts are just sketches - no effort was spent on parameter validation, error handling, etc.:

Code:
#!/usr/bin/ksh

typeset -l search="$1"                  # get commandline argument and convert to lower-case

sed -n 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
        /'"$search"'/,/constant/p' /path/to/inputfile

exit 0

The first sed-command converts everything to lowercase, after this the resulting pattern space is searched (and printed if in range) in the second line.

If you want the search to be case-insensitive but the output should still match the original input it gets a little more complex because we have to use the hold space as temporary buffer. We copy the pattern space to the holding space, perform the tolower-line on the pattern space, only then compare - and if we have a match, we move the hold space to pattern space, so that the line in its originally read form is printed.

Code:
#!/usr/bin/ksh

typeset -l search="$1"                  # get commandline argument and convert to lower-case

sed -n 'h
        y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
        /'"$search"'/,/constant/ {
             g
             p
        }' /path/to/inputfile

exit 0


Now for my second question: is there always only one range to be found in your file or could there be several? If there might be several the solution provided should work, but if there is only one possible to be found the script bears potential for optimization:

You see, "sed" works this way: it reads the first line of input, then applies one line of the script after the other (branching commands, etc., of course, apply) until it reaches the end of the script. Then the next line of input is read and the process is repeated. This continues until the last line is read and the script is applied for a last time, then sed finishes.

Now, suppose you have a file with 100 lines and your range (the only one) sits in line 20-30. If you could just quit after line 30 you could save a lot of processing time, yes? OK, lets do exactly that:

Code:
#!/usr/bin/ksh

typeset -l search="$1"                  # get commandline argument and convert to lower-case

sed -n 'h
        y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
        /'"$search"'/,/constant/ {
             /constant/ {
                 g
                 p
                 q
             }
             g
             p
       }' /path/to/inputfile

exit 0

The difference is we first define a new range inside our printing range. One which precisely matches the last line of the range. When we encounter this line we do as with the other lines in the range (copy hold space to pattern space, then print) but then we immediately quit, preventing the rest of the file to be processed.

I hope this helps.

bakunin

Last edited by bakunin; 06-18-2012 at 10:22 AM..
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

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

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. 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. Shell Programming and Scripting

How to handle case insensitive in shell scripting

Hi All, I am new to this forum also new to shell scripting. I have a requirement to read a text from a file which can be in any case like LOGFILE or LogFile or logfile or lOgfILE etc.. Can you guys help me out. Thanks, Bacsi01 (1 Reply)
Discussion started by: bacsi01
1 Replies

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

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

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. Shell Programming and Scripting

Case-insensitive serach with awk

Is there any way to do case insensitive search with awk for the below statement: month1=`awk '/month/' ${trgfile} | cut -d"=" -f2` the "month" could come as Month, mOnth,MONTH etc. in a file. Now I am looking for "month".... Thanks, AC (4 Replies)
Discussion started by: acheepi
4 Replies
Login or Register to Ask a Question