0

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

Silvio
  • 123
  • 1
  • 9
  • 1
    Please post the expected output too in your post now and let us know then. – RavinderSingh13 Jul 08 '18 at 11:19
  • BTW, just by looking at the documentation of csvkit, I feel like you used the wrong syntax. What about `csvgrep -c 1 -r '(28|29|30)$'` or something similar? The regex have to be properly adjusted. – Poshi Jul 08 '18 at 11:38
  • `2018-06-29`? This isn't usually the last day of June and what about leap years? – Cyrus Jul 08 '18 at 11:38
  • Yes, that's another issue. But looking at his code, it looks like he is extracting lines with 28, 29 and 30. – Poshi Jul 08 '18 at 11:40
  • This are historical prices for quotes from investing.com. So not all month, not all data which they deliver ends correct with the month for us. That's why I only need the last entry of a month. If month end 29, 28, 30 or what ever. Only the last entry in the csv file of the month. – Silvio Jul 08 '18 at 12:01

1 Answers1

2

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