How would I do string comparison in awk with variable strings on both sides?
As a follow-up to my previous question, how would a similar string comparison be made when there are mutable characters on either side of it in the document?
As an example:
# test,date
"test1","27 March 2024 01:37 PM UTC"
"test2","13 April 2024 07:08 AM UTC"
"test4","13 April 2024 07:09 AM UTC"
"test5","13 April 2024 07:10 AM UTC"
"test6","13 April 2024 07:12 AM UTC"
How would the previous answer:
awk -F, -v date="$(date +'%B %Y')"$" '/^[^#]/ && $2 ~ date{print $1}'
Be modified to print all the tests from this month (April 2024 at the time of writing)?
awk -F, -v date="$(date +'%B %Y')"$"
is creating a regexp April 2024"$
stored in the variable date
. That won’t match your new input so just tweak it to a regexp that does match by changing "$
to [^"]*"$
or whatever other matching regexp you prefer, e.g.:
$ awk -F, -v date="$(date +'%B %Y')[^"]+"$" '/^[^#]/ && $2 ~ date{print $1}' file
"test2"
"test4"
"test5"
"test6"
or possibly more robustly depending what other values can be in that 2nd field:
$ awk -F, -v date="$(date +'%B %Y') [0-9]{2}:[0-9]{2} [AP]M [[:upper:]]+"$" '/^[^#]/ && $2 ~ date{print $1}' file
"test2"
"test4"
"test5"
"test6"