Justin Garrison
January 6, 2024

Nu Shell Is Cool

Posted on January 6, 2024  •  2 minutes  • 258 words

I had a csv file with data that looked like this.

email,date
[email protected],Mon Jan 01 2024 15:59:34 GMT+0000
[email protected],Wed Nov 01 2023 23:20:09 GMT+0000

And I needed to convert the date field into this:

email,date
[email protected],2024-01-01
[email protected],2023-11-01

I tried to do this with awk first because it seemed like it would be a good fit. I’m sure it’s possible but the syntax is obscure and I couldn’t figure it out.

I ended up with something like this (not working):

awk -F',' '{ $1, system("/bin/date -d " %2 " +%Y-%m-%d")}' file.csv

When I can’t get something in a single for loop or a few pipes I’ll reach for a general purpose programming language. So I fired up iPython and wrote this to convert the file.

import csv
import datetime
from dateutil import parser

with open('file.csv') as f:
    reader = csv.reader(f)
    with open('file2.csv', "w") as f2:
        writer = csv.writer(f2, delimiter=",")
        for row in reader:
            dt = dateparser.parse(row[1])
            if type(dt) == datetime.datetime:
                line = row[0] + datetime.datetime.strftime(dt, '%Y-%m-%d')
                writer.writerow([row[0], datetime.datetime.strftime(dt, '%Y-%m-%d')])
            else:
                writer.writerow(row)

This works but it was more verbose than I wanted. I’m sure there’s other ways to do it, but it felt like something I should be able to do in my shell.

Finally, I tried it with nu :

open file.csv \
| update date {|row| $row.date | into datetime | format date "%F"} \
| save file2.csv

It was a more elegant and readable solution. I’m very impressed with how far nu has come and thankful for their helpful community who helped me quickly figure this out.

Follow me

Here's where I hang out in social media