There are some questions i have, because your object wasn't completely clear to me:
- 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.:
#!/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.
#!/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:
#!/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