Friday, July 17, 2009

How to move print jobs from one printer queue to another

In the olden days, an entire office department shared one printer. At home, an entire household shared one printer. Nowadays, I have 2 printers at home (1 HP LaserJet and 1 Samsung Monochrome Laser printer).

With access to multiple printers come new opportunities and challenges. One of those opportunities is that if one printer goes down for whatever reason, you can route your print jobs to the surviving one. One challenge is that you need to manage multiple printer queues. With more than 1 printer, I often find myself printing to the wrong one. How do you route print jobs from 1 printer to another when the need arises?

Job Submitted to Wrong Printer

Let's first consider this situation: you just submit your print job to the default printer(mySamsung), but you realize after the fact that it is the wrong printer.

First, you should find out the print job number. Run the familiar lpq command.
$ lpq -P mySamsung
mySamsung is ready and printing
Rank Owner Job File(s) Total Size
active peter 706 (i) 100352 bytes
1st peter 707 MoviesReview 228352 bytes

Note that given mySamsung is my default printer, I don't really need to explicitly specify the printer: lpq without any argument will do just fine.

Say job 707 is the one you want to move to the other printer.

The Linux command to do the actual migration is lpmove. Note that you need to run the command as root.

$ sudo lpmove mySamsung-707 myHP
$ lpq -P myHP
myHP is ready
Rank Owner Job File(s) Total Size
active peter 707 MoviesReview 228352 bytes

Note how I specified the job number to lpmove. The format is the printer name(mySamsung) and a dash(-), followed by the job number (707).

Printer Down

Suppose mySamsung is out of service for whatever reason. Then, you need to move all print jobs from mySamsung to myPrinter.

$ sudo lpmove mySamsung myHP

Thursday, July 2, 2009

Search and replace line feeds using emacs

As an avid emacs user for a decade plus, I am a little embarrassed to confess that I did not know for the longest time how to search for or replace line feed (newline) characters.

If you are only searching for run-of-the-mill ASCII strings such as abc, you just type C-s (control-S), and then the characters, and hit return.

Unfortunately, if your search string includes a line feed character, e.g., abc followed by a newline, I have some good news and some bad news for you.

The good news is that you use the same shortcut (C-s) just like you would normally to search for anything else.

The bad news is that the representation of what emacs understands to be a newline is not that intuitive ... even to geeks. Instead of something like \n which may mean something to developers, you need to enter C-q C-j (Control-Q, Control-J) for each line feed in the search string.

C-q is the shortcut for the quoted-insert command. The character you type immediately after C-q is escaped, i.e., its normal function is suppressed. C-j is the line feed character. Why the letter j, you ask? It turns out that the line feed character is represented by the decimal value 10 in the ASCII table, and j is the 10th letter in the English alphabet. Similarly, if you are searching for the Tab instead of the newline character, use C-q C-i to denote the Tab character.

If you want to replace occurrences of a string that includes one or more line feeds, the method is similar. Use the usual command replace-string or query-replace(M-%). Enter C-q C-j where you would expect to type the new line.
M-x replace-string (Hit return) abcC-qC-j (Hit return) def (Hit return)