Search code examples
bashsortingdate

Parsing csv file output before certain date


I am trying to sort and parse lines in a csv file. So far I have been able to sort the date column. Assuming I can use sort to filter from a certain date - but all the info I find deals with different date formats. Below is a sample of the file.

Here is how I have sorted the output by date so far:

sort -t, -k 3 timestamp.csv | grep @example.com

[email protected],active,20240716084550.486Z
[email protected],active,20240716084558.413Z
[email protected],active,20240716084608.601Z
[email protected],active,20240716084619.079Z
[email protected],active,20240716084626.169Z
[email protected],active,20240716084652.013Z

I would like to sort the output with all lines with a date string of "20240418" and before that date.

I tired sorting the output by the date column with:

sort -t, -k 3 timestamp.csv | grep @example.com

I am expecting to show output with a date column of 20240418 or before this date. I can just edit the file manually (what works for now) but would like to parse the file from the cli to save a step.

Thank you


Solution

  • After sorting, pipe to awk and exit when you get a line higher than your target date.

    sort -t, -k 3 timestamp.csv | awk -F, '$3 > 20240418000000 {exit} /@example.com/'