I have a script which is run a few million times in a single week, that simply finds the first cell in a CSV filed called file.csv
that matches $word
exactly, and prints the whole line, e.g. CSV:
robot@mechanical@a machine that does automated work
fish@animal@an animal that lives in the sea
tree@plant@a plant that grows in the forest
If one searched for "tree", then this would be printed:
tree@plant@a plant that grows in the forest
These two approaches get the same results:
awk -F@ -v pattern="$word" '$1 ~ "^" pattern "$" {print; exit}' file.csv
grep ^$word@ file.csv | head -1
Similarly, this can be used to check for an exact match in the second column of the CSV, assuming there are 3 columns:
awk -F@ -v pattern="$word" '$2 ~ "^" pattern "$" {print; exit}' file.csv
grep ^.*@$word@.*@.*$ file.csv | head -1
Given a choice of two scripts, such as this example above, which always produce exactly the same output, how can I quickly determine which will be faster?