Perl Script to Convert CUPS /etc/cups/printers.conf to CSV

I'm currently transferring a Formtrap…

Login

Blog History

I'm currently transferring a Formtrap Forms Server to a later version and I want to run the new and old servers in parrallel for a while so I have added a heap of printers onto a Redhat 5.5 Server pointing to the new server.

The trouble is that all the settings aren't visible in one view so I wanted to export printers.conf to CSV so I can view it in a spreadsheet.

Below is a Perl script that I wrote to do the conversion. IANAP so YMMV.

You may have to copy /etc/cups/printers.conf somewhere it is accessible.

The following script converts this:


Info Pallet Label Printer
Location Palletizing Area
DeviceURI lpd://print-server/cabpl
State Idle
StateTime 1334710494
Accepting Yes
Shared No
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
OpPolicy default
ErrorPolicy stop-printer


Info SSCC Label Print Test
DeviceURI lpd://print-server/printer2
State Idle
StateTime 1298006302
Accepting Yes
Shared No
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
OpPolicy default
ErrorPolicy stop-printer

To this:

Accepting,DeviceURI,ErrorPolicy,Info,JobSheets,KLimit,Location,OpPolicy,PageLimit,QuotaPeriod,Shared,State,StateMessage,StateTime,name,
Yes,lpd://print-server/bottpl,stop-printer,Formtrap Bottling Pallet Label Printer,none none,,Bottling Palletizing,default,,,No,Idle,,1334547108,cabpl,
Yes,lpd://print-server/cabpl,stop-printer,Formtrap Margarine Pallet Label Printer,none none,,Marg Palletizing,default,,,No,Idle,,1334710494,printer2,

Here is the Perl Script
You need edit the filenames in the script to match your input and output filename preferences

use strict;
use warnings;
 
my %printer_hash = ();
my $hash_key = '';
my %settings = ( name => 1 ); # store all possible settings
my $sep = $/; # store the current separator
my $input_file = 'printers.txt';
my $output_file = 'printer.csv';
#$/ = "\r\n";  # this is for a printer.conf with windows CRLF adjust as nessary for unix LF format
 
 
 
open ( FH, "< $input_file") or die "$? $!\n";
 
 
while (  ) {
	chomp($_);

	if ( $_ =~ //i ) {
		$printer_hash{$1}{name} = $1;
		$hash_key = $1;
		print "$printer_hash{$hash_key}{name}\n";
	} elsif ( $_ =~ /^(\w+)\s(.*)$/i ) {
 
		$printer_hash{$hash_key}{$1} = $2;
		#print "Printer : $hash_key Key : $1 Value : $2\n";
		$settings{$1} = 1;
	}
 
}
 
close (FH);
 
open ( CSV, ">$output_file") or die "$? $!\n";
 
 
foreach my $setting( sort keys %settings ) {
		 print CSV "$setting,";
	 }
print CSV "\n";
 
foreach my $key ( sort keys %printer_hash ) {
	#print "$key\n";
	foreach my $setting( sort keys %settings ) {
 
		 print CSV $printer_hash{$key}{$setting} ? "$printer_hash{$key}{$setting}" : "" ;
		 print CSV ',';
	 }
	 print CSV "\n";
}

2 Comments

  1. elvis

    should be if ( $_ =~ //i ) { without the space between the word and the regex.

    Reply
    • admin

      Thanks fixed it.

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.