Using awk to search case insensitive


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Using awk to search case insensitive
# 1  
Old 05-16-2019
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 !.


Code:
#!/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  $cur_dir *[Aa][Ww]*[Ss][Rr]*[Dd][Mm][Ll]*.lst

for SQLOutFile in `ls $cur_dir *[Aa][Ww]*[Ss][Rr]*[Dd][Mm][Ll]*.lst`;
do
          file_ctr=` expr   $file_ctr + 1 `
          printf "\n  %d : Processing the file  %s  \n"  ${file_ctr} ${SQLOutFile}
          echo $0

          starttime=`awk   '/START TIME/ {x=NR+5;next}(NR==x){print}'  toupper($SQLOutFile) `
          echo $starttime

         printf "\t  Start Time is   : %s %s \n"  `   awk '/START TIME/ {x=NR+5;next}(NR==x){print}'  ${SQLOutFile} | cut -d ' ' -f3,4 `

         done;

Error:
./test.sh
+ ./test.sh[17]: syntax error at line 1 : `(' unexpected

Thanks Siva SQL
# 2  
Old 05-16-2019
You are getting error since toupper() should be used inside awk. Why do you use toupper() ? (You are already fetching filenames in upper or lower case)
Quote:
for SQLOutFile in `ls $cur_dir *[Aa][Ww]*[Ss][Rr]*[Dd][Mm][Ll]*.lst`;
# 3  
Old 05-17-2019
In addition to what anbu23 has already said, note that if the file name you are processing contains any lowercase letters, converting those lowercase letters to uppercase would cause awk to fail with an error noting that no file with the converted filename exists.
# 4  
Old 05-17-2019
I have to search perform on the file content into case-insensitive search; that's why using toupper()
# 5  
Old 05-17-2019
Quote:
Originally Posted by Siva SQL
I have to search perform on the file content into case-insensitive search; that's why using toupper()
Please be much more explicit about what you are trying to do. The awk toupper() function converts lowercase letters in the string it is given as an argument to uppercase letters. It does not change characters found inside a file whose name is specified by its operand. And, as stated before, toupper() is an awk function, not a shell utility.

If you want to convert the contents of a file to change lowercase letters within a file to uppercase letters while preserving the original ownership, access modes, and links you can use either of the following:
Code:
TmpFile=${0##*/}.$$
awk '{print toupper($0)}' "$SQLOutFile" > "$TmpFile" &&
    cp "$TmpFile" "$SQLOutFile" &&
    rm "$TmpFile"

If you're trying this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
or:
Code:
TmpFile=${0##*/}.$$
tr '[:lower:]' '[:upper:]' < "$SQLOutFile" > "$TmpFile" &&
    cp "$TmpFile" "$SQLOutFile" &&
    rm "$TmpFile"

If what you want is to perform a case insensitive match when looking for the string START TIME, but you want the output produced by your awk script to match the case sensitive input found in the file, change:
Code:
          starttime=`awk   '/START TIME/ {x=NR+5;next}(NR==x){print}'  toupper($SQLOutFile) `

to:
Code:
          starttime=$(awk   'toupper($0) ~ /START TIME/ {x=NR+5;next}(NR==x)'  "$SQLOutFile")

or to:
Code:
          starttime=$(awk   '/[Ss][Tt][Aa][Rr][Tt] [Tt][Ii][Mm][Ee]/ {x=NR+5;next}(NR==x)'  "$SQLOutFile")

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Case insensitive file name search and replace

I am trying to find case insensitive file names and then replace that particular file with other name. if then ls | grep -i "update" | xargs -I {} mv {} LineItems.csv echo "File moved from *update*" elif then ls | grep -i "priority" | xargs -I {} mv {} ... (1 Reply)
Discussion started by: ATWC
1 Replies

2. UNIX for Dummies Questions & Answers

Command for a case insensitive search

Hi All, What is the command to search a file for a case-insensitive match 1.grep -nc text filename 2.grep -i text filename 3.grep -i filename text 4.grep -nc filename text 5.grep -c text filename Thanks for your help (1 Reply)
Discussion started by: bobby1015
1 Replies

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

4. UNIX for Dummies Questions & Answers

Using FIND with case insensitive search

I am using HP-Unix B.11.31. Question: How to do the case insensitive search using FIND? Example: I would like list the files with extension of *.SQL & *.sql. When I try with command find . -type f -name *.sql, it does not lists file with *.SQL. (5 Replies)
Discussion started by: Siva SQL
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. AIX

Case insensitive search in AIX man ?

Hello, Linux man command search is case insensitive by default, but not AIX man. How do I serch case insensitive while using AIX manual pages ? thanks Vilius (7 Replies)
Discussion started by: vilius
7 Replies

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

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

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

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