Justpee
Justpee

Reputation: 191

Ignore case sensitive in AWK Shell

awk -F':' 'BEGIN{IGNORECASE=1}/' '$1~/'$name'/' $file

It seems to have "runaway regular expression" error

How do i add in ignorecase for such cases?

 John:English:95:May
 May:Math:99:John
 Peter:Math:55:John
 May:Sci:76:John

my $file data

Please enter your name: may

User input

May:Math:99:Joh
May:Sci:76:John

Expected result

Upvotes: 1

Views: 2264

Answers (1)

Inian
Inian

Reputation: 85580

You can use a POSIX compatible string functions tolower() and topper() for case-insensitive look-ups in Awk rather than using GNU Awk specific IGNORECASE which doesn't provide flexibility to change the case for specific needs but only as a whole.

Assuming your $name is read from user-input in a bash variable, you can do,

awk -F":" -v name="$name" 'tolower($1) ~ tolower(name)' file
May:Math:99:John
May:Sci:76:John

(or) if you intend to use IGNORECASE, do something like,

awk -F":" -v name="$name" 'BEGIN{IGNORECASE=1} $1 ~ name' file
May:Math:99:John
May:Sci:76:John

This is assuming you are reading the user-input in a shell command using read and having the value stored in the variable $name in shell context.

Upvotes: 2

Related Questions