sc: the Venerable Spreadsheet Calculator

 in
If you like vi, and you like the command line, you will love sc—a spreadsheet that runs in a terminal.

Boy, there sure is a lot of software for Linux—a lot of software! Why, if you want a browser, you can choose between Firefox, Opera, Chrome, Galeon, Surf and many others. And, on the command line, wget, curl, Lynx, ELinks and more are available. For e-mail, options include Evolution, KMail, Balsa, and xmh; or on the command line, you can use mutt (my favorite), sup, pine, mh and countless more. Calendaring choices include the GNOME calendar, KDE calendar, xcal and Evolution; or, in a terminal, you can use the very powerful Remind or ccal, not to mention command-line interfaces to the Google calendar. And for spreadsheets, you've got OpenOffice.org's OOCalc, Gnumeric, KSpread and Xspread; or, in a terminal, you've got perhaps the best spreadsheet of all, sc, especially if you're a vi fan.

I've been using sc for years, mostly for budgeting and project planning. The earliest version I've found was posted to comp.sources.unix on August 18, 1987, by Robert Bond (see Resources), but that was already version 4.1. The post said it was previously known as vc, and that the original version was written by James Gosling (of Java language fame) in September 1982. Although documentation for sc can be hard to find, it does come with a decent man page and a neat tutorial, which you can load right into sc. It also uses the same file format as Xspread, so existing documentation on formulas in Xspread (which is more plentiful than for sc) also can be helpful.

Basic Usage

If you use Debian or Ubuntu, just type sudo apt-get install sc. If your distribution doesn't have an sc package, see Resources for a link to the source. Start the program by typing sc in a terminal, and you'll see a screen that looks something like Listing 1. Because it's curses-based, you can run it over slow links, as well as inside screen, so that you can detach and re-attach from another terminal. There is a pretty detailed man page, which (in the Ubuntu package) points out that you can start up sc with a tutorial by doing this:

sc /usr/share/doc/sc/tutorial.sc

Actually, that isn't quite right. In Ubuntu, first you need to uncompress the tutorial:

sudo gunzip /usr/share/doc/sc/tutorial.sc.gz

Then start it.

If you prefer to get started immediately with some real data, here are some useful commands. Like vi, sc starts in a command mode. The vi movement keys, hjkl, move left, down, up and right among cells, as you would expect. To jump straight to cell D3, press gD3. You can begin entering a numeric value or formula using =. To interrupt a command gracefully, press Ctrl-G. See the Basic sc Usage in Command Mode sidebar for more simple commands.

To me, the three most important things about working with spreadsheets are the ease of adding new data, moving data and defining simple formulas that are re-calculated automatically. sc shines as far as the first two requirements with its vi-like command mode. It also does quite well with formulas. Check the on-line help for a sizable list of formulas, but the most common function in my experience is simple addition. This is no different in sc from any other spreadsheet.

To put the sum of the values in A3 through A10 into A12, go to cell A12, and type =@sum(A2:A10). To edit the formula, press e for edit, and you will be in command mode in the top line, editing the formula. Edit as you would in vi, and press Return to save the edited formula.

If you want to insert five more rows before row 5, go to cell A5, and type 5ir, which means “do 5 times: insert a row”. The formula (now in A17) will be updated automatically to read @sum(A2:A15). Now you can copy that formula into cell B17 by going to A17, typing ma, then going to B17 and typing ca. The formula will be updated automatically to read @sum(B2:B15).

If you want to highlight some specific data in the spreadsheet without actually having to delete rows, you can hide the uninteresting ones. This is called zapping in sc, and you do it by pressing Z followed by either r for row or c for column. (If you follow it with a Z instead, sc will assume you meant save and exit as ZZ does in vi.) You can un-hide by using S for show, followed again by r or c. Again, you can type 30Zr to zap 30 rows.

I've already mentioned the tutorial and detailed man page, but sc also has on-line help, which you can see by pressing ?. There, you will find settings you can toggle, ways to move the cursor, lists of financial functions and so on.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Code patch with indentation corrected

Ronald Greenberg's picture

Oops, I didn't realize the code would be reformatted. Should have used the right HTML tags to keep the indentation:

        col = letters[column]
        if len(p) != 0:
            try:
                n = string.atof(p)
                text.append('let %c%d = %g' % (col, row, n))
            except:
                if p[0] == '"':
                    text.append('label %c%d = %s' % (col, row, p))
                else:
                    text.append('label %c%d = "%s"' % (col, row, p))
        column += 1

Improvement to script for empty cells

Ronald Greenberg's picture

The script skips over empty cells as if they didn't exist. It's fine to not put anything in the .sc file for an empty cell, but one should move over to the next column so that empty cells are preserved. A fix is to replace the body of the "for p in allp" with this:
col = letters[column]
if len(p) != 0:
try:
n = string.atof(p)
text.append('let %c%d = %g' % (col, row, n))
except:
if p[0] == '"':
text.append('label %c%d = %s' % (col, row, p))
else:
text.append('label %c%d = "%s"' % (col, row, p))
column += 1
(I've aso applied the fix suggested in the previous comment.)

R.

Great article. Problem in Python script:(

dfranz's picture

Just on the day I started exploring sc on my Nanonote,
which was highligted in the same issue, I discovered this
really helpful article via web search. When my October
issue came in, I'm sure I saw the article, but it did
not catch my attention then:)

My quick review of sc revealed that there was no feature
to import from other formats. The Python script looked
like it solved the problem, but on my first test I found
that it treated all numbers containing a decimal point
as labels--not what I wanted. Changing the call
to string.atol() to string.atof() and changing
the format "let %c%d = %d' to 'let %c%d = %g'
solved the problem.

Now on to exploring sc on the Ben Nanonote. Thanks
for a timely article.

Delbert

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix