Can you crack the form feed challenge?
AnthonyEnglish 270000RKFN Comments (4) Visits (3767)
In a perfect world, form feeds would be invisible. We'd just print and a new page would spit out just at the right time. Alas, I've come across someone who thought it a great idea to hard-code a form feed at the beginning of each print job. My initial challenge was to get rid of it. Why? Because a form feed at the start of a print file means a blank page before the real print job.
If you edit the file that gets sent to the printer, using the trusty vi editor, you see something like this:
Get the idea?
Strictly speaking, it's not caret (^) followed by capital L. It's a ctrl-L. You can get that by editing a file using vi and typing ctrl-v followed immediately by ctrl-l. (Hold the control key down, press the letter v, then hold the control key down and press the letter l).
The end user is, naturally enough, asking why every time they print the job starts with a blank page
Create a script which will be called by the sed command:
1s On line 1 of the file, substitute ^L with nothing. (The / is a delimiter, so that sed knows where the pattern to search for ends and the pattern to replace it with begins).
So, there's the sed script. Let's call it sedscript
But someone, somewhere along the line, allowed the user to print multiple copies of a file. And that was not by sending two copies of the file to the printer. No. It was by concatenating file one (which, you'll remember, starts with a ^L) and file two. The result is:
Or, if you like:
The Double Form Feed Challenge
So now the task at hand is not to strip the ^L from the top of the file (the blank page at the start of the print job), but from within the file.
We don't want to remove every ^L. We only want to remove ^L at the start of the file and when you open the file and see two lines like this:
In other words, we want to remove the second control-L.
Now, perhaps you'll suggest something like: "just use sed, or tr, or perl, or awk" and I'm happy to do any or all of them. But how? What do I search for and replace it with? I can run sed twice, for example:
cat original_file | sed -f sedscript | sed -f sedscript2 | lp -d queue_name
But what should sedscript2 look like?
It would be so nice to be able to say to the original program: "turn off initial form feed". It would be delightful to uncheck a box and not have that blank page spitting out the top and mysteriously before every copy when they've asked for multiple copies. But changing the original program don't seem to be an option, so we have to reverse engineer.
When I say "we", I mean "you".
But I'm going to make it easy for you. First, here's a link to the AIX documentation - the man pages - for your benefit and edification. There you'll find links to awk, sed, perl, tr, cut, cat, grep and any other command you might like to check out.
Secondly, I'm not going to moderate comments. Your ideas will be posted immediately. I suppose I could get enthusiastic and issue a certificate for the first person who cracks the double form feed challenge. I've certainly got enough blank paper for it.