Home > Mobile >  Obtain month from date field using regex
Obtain month from date field using regex

Time:05-29

I have the file transactions.csv with a field called "Date" in mm/dd/yy

"Date"
4/22/2020
5/14/2020
12/04/2021

It is necessary to consider that there are more fields, my dataset is like this:

| ID      | customer | Date      |
|:----    |:------:  | ---------:|
| 147-48E | Andrew   | 4/22/2020 |

What I want is to remove the day and year using regex in order to have something like this:

| ID      | customer | date      |
|:----    |:------:  | ---------:|
| 147-48E | Andrew   | 4         |

I tried the following expression but is not working:

sed -E 's/([0-9]{2})\/.*\/.*/\1/' transactions.csv

How can I fix it and update my csv replacing the current Date field?

CodePudding user response:

No need for sed if cut will cut it:

cut -d/ -f1 transactions.csv

CodePudding user response:

The problem with your regex is that you have no literal periods in there.

\. will never match.

$ sed -E 's/^([0-9]{1,2}).*/\1/' transactions.csv
"Date"
4
5
12

CodePudding user response:

Using awk

$ awk '{for (i=1;i<NF;i  ) {if($i~/Date|[0-9] \/[0-9] \/[0-9] /){gsub(/\/.*/,"        ",$i)}}}1' input_file
| ID      | customer | Date      |
|:----    |:------:  | ---------:|
| 147-48E | Andrew | 4         |

Using sed

$ sed 's~\([0-9]\ \)/[0-9/]*~\1        ~' input_file
| ID      | customer | Date      |
|:----    |:------:  | ---------:|
| 147-48E | Andrew   | 4         |

CodePudding user response:

You could try:

sed -E 's@([0-9]{2})/[0-9]{2}/[0-9]{4}@\1@g' transactions.csv
  • Related