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.
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
The -I flag is only in GNU sed and only with the substitute command, not with a range match.. Try:
note: if $variable contains regex expressions then this may not work as expected..
I tried
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
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.:
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.
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:
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.
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)
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)
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)
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)
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)
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)
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)
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)