zigford.org/vim-odd-jobs-1.html
2020-07-21 06:49:32 +10:00

147 lines
5.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="main.css" type="text/css" />
<link rel="stylesheet" href="blog.css" type="text/css" />
<link rel="alternate" type="application/rss+xml" title="Subscribe to this page..." href="feed.rss" />
<title>Vim Odd Jobs 1</title>
</head><body>
<div id="divbodyholder">
<div class="headerholder"><div class="header">
<div id="title">
<h1 class="nomargin"><a class="ablack" href="http://zigford.org/index.html">zigford.org</a></h1>
<div id="description"><a href="about.html">About</a><a href="links.html"> | Links</a><a href="scripts.html"> | Scripts</a><br>Sharing linux/windows scripts and tips</br></div>
</div></div></div>
<div id="divbody"><div class="content">
<!-- entry begin -->
<h3><a class="ablack" href="vim-odd-jobs-1.html">
Vim Odd Jobs 1
</a></h3>
<!-- bashblog_timestamp: #201903301416.24# -->
<div class="subtitle">March 30, 2019 &mdash;
Jesse Harris
</div>
<!-- text begin -->
<p>Today Clarissa did the shopping and as she was in a rush didn't have the time
to total the items in the trolley. She had updated the cost of each item in a
shared iCloud note and texted me to see if I could quickly add them up on my
computer. </p>
<hr />
<p>This is the point where most people would pull out <code>calc.exe</code> and manually sum
them. Not I!.</p>
<p>I fired up the iCloud site on my machine and copy and pasted the iCloud note to
a fresh vim buffer:</p>
<pre><code> Shopping 2019
1st Isle:
Vedge
Potatoes 4 + 6 6 3 3 5 16.99 2 2 3 3 4.99 5.99 5.99 5.99 12.15 7.99 7.99 8.99
Other vedge for a meat + vedge dinner
Meats
Chicken dinner with a sauce (Indian or Apricot or some other chicken + rice meal)
Lunch Chicken
Sausages
Kids Junk Food
Tiny Teddies (Olivias request) 2.49 2.49
Flavoured Rice Cakes 6
Crackers 2.49 2.49
Dairy 2.69
Big tub of greek yogurt
Vanilla Bean Yogurt 3.99 3.99 3.99
Butter - 4.99 4.99 4.99
Milk Powder - 5.69 5.69 5.69
Second Isle
Corn Chips for Nachos
Nacho Salsa
Popcorn 2.49 2.49 9.98
A Cordial or somesuch 2.49
Third Isle
Frozen
Pies 3.55 3
Lasagne? 12
French Fires 1.89 1.89
Chicken Nuggets 2.99 2.99
Cleaning
Garbage Bags 1.79
Mozzie Coils
Last Isle
Food
Spaghetti Pasta .65 .65 1.59 1.59
Tinned Beetroot 1.15
Tinned Spagetti * .85 .85 .85
Indian or rice meal sauce 2.49 .99
Personal
Soap 1.99 3.49
Food
Brown Sugar 2.69
Bread 1.25 1.25
Wraps 1.95 1.95
2.99
8.49
1.99 1.99 1.99
</code></pre>
<p>My thought was this:</p>
<ul>
<li>Do a search and replace on everything bar the numbers</li>
<li>Quickly tally the numbers using <a href="https://www.gnu.org/software/bc/manual/html_mono/bc.html"><code>bc</code></a></li>
</ul>
<h3>Step 1. Find the search</h3>
<p>Best way to figure out a regular expression in vim is to enable these two
settings:</p>
<pre><code> :set hlsearch incsearch
</code></pre>
<p>With these set, you can begin a search by typing <code>/</code> then as you type results
are instantly highlighted.</p>
<p>After a few tries with negative lookbehinds and whatnot, I settled on this:</p>
<pre><code> /\v[a-zA-Z()+*?\- :]
</code></pre>
<p>The <code>\v</code> causes Vim to use a more modern regex pattern requiring far less
escape characters.</p>
<h3>Run the search</h3>
<p>I then ran the search of the whole file:</p>
<pre><code> :%s/\v[a-zA-Z()+*?\- :]/ /g
</code></pre>
<p>Then all I had to do was clean up double spaces and the blank lines</p>
<pre><code> :%s/\v^\s+//g " removing leading spaces
:g/\v^$/d " delete blank lines
</code></pre>
<h3>Convert it to a format that bc would like</h3>
<pre><code> :%s/\v\s/+/g " replace spaces with +
:%s/\v\n/+/g " replace newlines with +
:%s/\v\+$//g " remove any trailing +'s
</code></pre>
<h3>Tally with BC</h3>
<p>You can do this from within Vim using the <code>!</code> command or drop the shell and
do it</p>
<pre><code> :!cat % | bc
</code></pre>
<p>Now all that may seem a little over the top for tallying a shopping list,
however it's little exercises like this that can help you learn more vim,
regex, shell and other useful things</p>
<h4>Extra credit</h4>
<p>You could of course you another tool to add the numbers up. Here is how to do
it using <code>awk</code> having each number on a seperate line:</p>
<pre><code> :!awk '{n+=$1}; END{print n}' %
</code></pre>
<p>Using powershell <em>each number on seperate line</em></p>
<pre><code> :!pwsh -com "&amp;{gc %|\%{[double]\$n+=\$_};\$n}"
</code></pre>
<p>Credit for awk and bc goes to this <a href="https://askubuntu.com/questions/785038/how-can-i-sum-numbers-on-lines-in-a-file">askubuntu</a> answer.</p>
<p>Tags: <a href='tag_vim-tips.html'>vim-tips</a>, <a href='tag_vim-odd-jobs.html'>vim-odd-jobs</a></p>
<!-- text end -->
<!-- entry end -->
</div>
<div id="footer">&copy <a href="http://twitter.com/zigford_org">Jesse Harris</a> &mdash; <a href="mailto:jesse&#64;zigford&#46;org">jesse&#64;zigford&#46;org</a><br/>
Generated with <a href="https://github.com/cfenollosa/bashblog">bashblog</a>, a single bash script to easily create blogs like this one</div>
</div></div>
</body></html>