Always use boundaries for exact matches

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the regex category.

Last Updated: 2024-03-02

When looking for exact matches, do not use grep without before and after boundaries - otherwise you'll get unwanted substrings.

I had a bug in a shell script that acted as a simple, file-based DB

Here is how the date is stored:

# format: file name<|>retrieval_id

Here was my incorrect code

retrieval_id=$(grep "$file" storage.db | tail -n 1 | awk -F "$delimiter" '{print $3}')

The issue was that this matched for substrings of file names. I.e. it matched not only for the file tags but also for the file tanktop

The fix was to specify that the match must be bounded by the start of the line and the delimiter.

retrieval_id=$(grep "^$file""$delimiter" storage.db | tail -n 1 | awk -F "$delimiter" '{print $3}')