bashshellcsvcsvkit

Last Day of Month in csvfile


i try to delete all days of a csv file which not matched last days. But I find not the right solution.

date,price
2018-07-02,162.17
2018-06-29,161.94
2018-06-28,162.22
2018-06-27,162.32
2018-06-12,163.01
2018-06-11,163.53
2018-05-31,164.87
2018-05-30,165.59
2018-05-29,165.42
2018-05-25,165.96
2018-05-02,164.94
2018-04-30,166.16
2018-04-27,166.69

The output I want become

date,price
2018-06-29,161.94
2018-05-31,164.87
2018-04-30,166.16

I try it with cut + grep

cut -d, -f1 file.csv | grep -E "28|29|30"

Work but bring nothing when combine -f1,2.

I find csvkit which seem to me the right tool, but I find not the solution for multiple grep.

csvgrep -c 1 -m 30 file.csv

Bring me the right result but how can combine multiple search option? I try -m 28,29,30 and -m 28 -m 29 -m 30 all work not. Best it work with last day of every month.

Maybe one have here a idea.

Thank you and nice Sunday Silvio


Solution

  • You want to get all records of the LAST day of the month. But months vary in length (28-29-30-31).

    I don't see why you used cut to extract the first field (the date part), because the data in the second field does not look like dates at all (xx-xx).

    I suggest to use grep directly to display the lines that matches the following pattern mm-dd; where mm is the month number, and dd is the last day of the month.

    This command should do the trick:

    grep -E "01-31|02-(28|29)|03-31|04-30|05-31|06-30|07-31|08-30|09-31|10-30|11-31|12-30" file.csv
    

    This command will give the following output:

    2018-05-31,164.87
    2018-04-30,166.16