Reputation: 43
I am trying to print occurrences of a field in a .csv file only using an awk command. For example in a file test.csv like so:
layla;rebel;TAT
han_solo;rebel;TAT
darth_vader;empire;DKS
yoda;rebel;TAT
with the command:
cat test.csv | ./how_many_are_we.sh dks
I wish to have the following output:
1
Here is my code in how_many_are_we.sh (which is working but case-sensitive):
#! /bin/bash
awk -F ";" -v location=$1 'BEGIN {count=0;} { if ($3 == location) count+=1} END {print count}'
I tried adding IGNORECASE=1
in different places but I can't seem to find the right way to make it work.
Excuse my bad wording, and thank you for the help.
Upvotes: 4
Views: 1901
Reputation: 133508
You could change the case of entered value and for 3rd field to lower case and then compare their values to make sure however they are entered comparison shouldn't be affected.
#!/bin/bash
awk -F ";" -v location="$1" 'BEGIN {location=tolower(location);count=0;} { if (tolower($3) == location) count+=1} END {print count+0}' Input_file
OR As per Glenn sir's comments use shell trick to make it lowercase in variable itself.
#!/bin/bash
awk -v location="${1,,}" 'BEGIN{FS=";"} (tolower($3) == location){count+=1} END{print count+0}' Input_file
OR more awk
sh way change awk
command to following(above is OP's command fix this is to make it awk
sh style)
awk -v location="$1" 'BEGIN{location=tolower(location);FS=";"} (tolower($3) == location){count+=1} END{print count+0}'
NOTE: For using IGNORECASE=1
, you should mention it either in BEGIN
section like BEGIN{IGNORECASE=1}
OR like an awk
variable -v IGNORECASE="1"
.
Also on a side note, OP's shebang is having spaces between #!
and /bin/bash
which shouldn't be the case so I had fixed that too here.
Upvotes: 6
Reputation: 26481
Normally, if you have a script that can work easily on lower-case input, you can convert the input stream with tr
, so it can also manipulate mixed-case input.
$ command | tr '[:upper:]' '[:lower:]' | ./script
However, if you want to mask the tr
, you can embed it into your script by default using the following redirection:
#!/usr/bin/env bash
exec 0< <( tr '[:upper:]' '[:lower:]' )
# the rest of your script comes here:
awk '...'
which now allows you to do something like:
$ command | ./script
Upvotes: 0