As mentioned last week, I wanted to change my account names to lowercase. For example, instead of my current:
Expenses:Groceries
I wished to have:
expenses:groceries
I also had waxed eloquently how sed would be the tool for this job. Turns out, I was wrong, at least for my level of understanding of sed. That is, there is a way in sed to change to lowercase, but it’s not trivial, especially if you don’t want to change everything to lowercase.
And I wanted to make some things lowercase but not others. For example, if I had an account at Acme Bank, I would want the account name to appear as:
Acme Bank
Not
acme bank
Therefore, I needed to review a list of all the account names in my hledger file, to select which accounts I wanted to change.
Fortunately, hledger has the aptly named “accounts” command, that lists all the accounts in a file. This lists the account names with the current capitalization, which is exactly what I needed. Warp, which I have mentioned in some earlier postings, made it very easy to copy the output of the file. So I copied the account names into my Emacs text editor.
Now, with the list of account names, I could build a list of sed commands to change from the original capitalization to lowercase.
The list of sed commands would look something like:
s/Dining Out/dining out/g
s/Drugstore/drugstore/g
s/Education/education/g
etc.
If you are not a sed user, then it’s worth explaining that the first command in the above list means:
Every time you find “Dining Out,” substitute “dining out” instead.
Note that I don’t want to make any changes to Acme Bank, so I don’t need anything like:
s/Acme Bank/Acme Bank/g
The above would just substitute the identical text for itself. Useless.
So, I removed each account that had capitalization that I wanted to keep. Just to review, part of my list would have:
Dining Out
Drugstore
Education
But it wouldn’t include:
Acme Bank
My next step was to use a spreadsheet, copying the filenames as follows:
Next, I need the same names, but in the corrected lowercase version. I used Excel’s lower() function, but I could have lowercased the words in a text editor or word processor and pasted them in. Here they are:
Now it was time to add in all the other elements of the sed commands that I needed:
Almost, there! Finally, I used Excel’s concat() function to bring all the above together in a new column:
Next, I pasted the above cells in column F into Emacs, and I ended up with:
s/Dining Out/dining out/g
s/Drugstore/drugstore/g
s/Education/education/g
Of course, my actual file was much longer. And the beauty of this is that I give sed the file of commands one time, and it executes all the changes at once.
Two things before I end this week’s blog:
- I haven’t yet changed my actual hledger data. I am still looking at the changes sed would make before I decide if I am happy with the changes. So far, they look good, but who knows what I will find? Slow and sure is my motto here.
- There are certainly other ways, some of them undoubtedly more efficient, to make certain account names lowercase but leaving others uppercase. However, I used the tools that I knew (mostly) how to use.