Saturday, 15 March 2014

bash - Case insensitive comparision in If condition -



bash - Case insensitive comparision in If condition -

i have csv file , need count number of rows satisfies status row entry betwen year range , artist_name matches name argument. string matching should case insensitive. how accomplish in if loop..

i beginner, please bear me

#!/bin/bash file="$1" artist="$2" from_year="$(($3-1))" to_year="$(($4+1))" count=0 while ifs="," read arr1 arr2 arr3 arr4 arr5 arr6 arr7 arr8 arr9 arr10 arr11 ; if [[ $arr11 -gt $from_year ]] && [[ $arr11 -lt $to_year ]] && [[ $arr7 =~ $artist ]]; count=$((count+1)) fi done < "$file" echo $count

the $arr7 =~ $artist part need create modification

the bash case-transformations (${var,,} , ${var^^}) introduced (some time ago) in bash version 4. however, if using mac os x, have bash v3.2 doesn't implement case-transformation natively.

in case, can lower-cased comparing less efficiently , lot more typing using tr:

if [[ $(tr "[:upper:]" "[:lower:]" <<<"$arr7") = $(tr "[:upper:]" "[:lower:]" <<<"$artist") ]]; # ... fi

by way, =~ regular look comparison, not string comparison. wanted =. also, instead of [[ $x -lt $y ]] can utilize arithmetic compound command: (( x < y )). (in arithmetic expansions, not necessary utilize $ indicate variables.)

bash shell

No comments:

Post a Comment