Just moozing

Before you can check your notes, you must make them…

Command line coolness

leave a comment »

Sometimes it is just obvious that command line is superior to GUI for certain tasks…

Example 1: Merging csv files

I download 63 csv files from my solar power inverter. How to merge that into one big file? The problem was that the first 10 lines were some sort of metadata, that I wanted to get rid of.

The traditional way, IĀ suppose, would be to open each file individually, remove the first lines and copy the rest into the master csv file – and do that for each csv file.

On the command line

tail -q -n +10 *.csv > merged.csv

Using the tail program I cut the first 10 lines from the file before it is send to stdout, do this for all csv file and dump stdout to merged.csv.

A cross ref comment, tail is in coreutils and a handy link is theĀ Tail man page.

Example 2: Merging PDF documents

At work, we have a crappy Ricoh Afficio MP4500 printer/scanner/whatever (aka. the spaceship). Whenever I scan multiple documents manually, it logs me out every 20-30 seconds – and that is really annoying. I get reminded of it every time we have exams, since I do exams notes in my A5 notebook, and then, to keep my notes for posterity, I scan them and mail them to myself. With this logging out “feature”, it is a bad experience.

So, I was suggested to do this thing with multiple pages in the top paper feeder, and switch on the “scan both sides” feature. This resulted in instant paper jam – three times in a row – so I decided, it was not the way to go either.

What to do? I went for multi page single sided to get the A-sides, and then turn the pile and do it again to get the B-sides. This gave me A.pdf and B.pdf. Scanning single side and emailing it works well – when you know the quirks and only do it once in a while.

Yes, I really have a strained relationship with our spaceship.

Anyway, solving getting the two files merged, I went for pdftk.

pdftk B.pdf cat end-1 output B_reverse.pdf
pdftk A=A.pdf B=B_reverse.pdf shuffle A1-end B1-end output merged.pdf

The first command is about taking the entire PDF and reverse the pages. The second command builds a new PDF by taking pages from the two files alternately. With these two, we end up with a correct ordered PDF without relying on the faulty “both sides scanning”. Maybe this can be done as one-liner, but I didn’t delve on it.

Pdftk is in the package pdftk, and can also be found at the pdflab website.


Written by moozing

June 21, 2012 at 09:00

Posted in Tech

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: