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
# 8  
Old 06-18-2012
Quote:
Originally Posted by johnjs
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.
what is your output?

@Scrutinizer's code is correct to me..
because of this code converts to lower (or upper ) for any input (does not matter if it is lower case or upper)
and if it matches then write..

Code:
# cat file
Test
first
1
2
3
endpattern
Test1
Test2
FIRST
4
5
6
endpattern
Test3
Test4
FiRsT
1
2
3
endpattern
XXXXXX
XXXXXXXX

for exa your input variable "first"..
Code:
# awk 'toupper($0)~toupper(s),/endpattern/' s="first" file
first
1
2
3
endpattern
FIRST
4
5
6
endpattern
FiRsT
1
2
3
endpattern

for exa your input variable "FIRST"
Code:
# awk 'tolower($0)~tolower(s),/endpattern/' s="FIRST" file
first
1
2
3
endpattern
FIRST
4
5
6
endpattern
FiRsT
1
2
3
endpattern

if you use the solaris then you must try the with the nawk or /usr/xpg4/bin/awk
This User Gave Thanks to ygemici For This Post:
# 9  
Old 06-18-2012
Lateral thought method: Find out the line numbers containing each case-insensitive match to $variable and use sed to display from that line number until the the next occurence of endpattern. Does not work if $pattern just contains a number.
Code:
cat -n file | grep -i "${variable}" | awk '{print $1}' | while read n
do
     sed -n "$n,/endpattern/ p" file
done

Grossly inefficient for large files and/or where there are many hits.

Last edited by methyl; 06-18-2012 at 11:05 AM.. Reason: layout and grammar
# 10  
Old 06-18-2012
Thanks Bakunin.. Please find the sample case below. if i have a file like below

FIRST:\
one
two
three
end:

second:\
one
two
three
end:

case 1: If i gave search variable as "first" then it should match and print the o/p like below

FIRST:\
one
two
three
end:

case 2: If i gave search variable as "SECOND" then also it should match and print the o/p like below

second:\
one
two
three
end:

case 3: If i gave search variable as "FIRST" then it should match and print the o/p like below

FIRST:\
one
two
three
end:

case 4: If i gave search variable as "second" then also it should match and print the o/p like below

second:\
one
two
three
end:
# 11  
Old 06-18-2012
Hi, I get:

Code:
$ variable=first; awk 'tolower($0)~tolower(s),/end:/' s="$variable" infile
FIRST:\
one
two
three
end:
$ variable=SECOND; awk 'tolower($0)~tolower(s),/end:/' s="$variable" infile
second:\
one
two
three
end:
$ variable=FIRST; awk 'tolower($0)~tolower(s),/end:/' s="$variable" infile
FIRST:\
one
two
three
end:
$ variable=second; awk 'tolower($0)~tolower(s),/end:/' s="$variable" infile
second:\
one
two
three
end:

What do you get?




--
On Solaris use /usr/xpg4/bin/awk rather than awk
This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 06-18-2012
it works

awk 'tolower($0)~tolower(s),/endpattern/' s="FIRST" file

the baove comamd works..
thanks for all your help..
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