
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on
05-30-2017
08:01 AM
- last edited on
03-04-2025
07:56 AM
by
kh-richa_mig
Im sure there is a grep and lpadmin command that will allow this, but before reinventing the wheel I figured I'd ask if anyone has already created this.
I have printers that all start with "HS-" added on our staff computers which we now need to delete so we can let users add the printers with our newer print server in self service.
So I was trying to get a script to just target the ones with those names and leave their others in tact.
Gabe Shackney
Princeton Public Schools
Princeton Public Schools
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 05-30-2017 08:07 AM
Something like
lpstat -v | awk '/HS-/{print substr($3,1,length($3)-1)}' | xargs -I{} lpadmin -x {}
could work.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 05-30-2017 08:07 AM
Something like
lpstat -v | awk '/HS-/{print substr($3,1,length($3)-1)}' | xargs -I{} lpadmin -x {}
could work.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 05-30-2017 08:25 AM
Syntax error in regular expression HS-{print substr($3)-1)} at } source line number 1
Is the error I get with that.
Gabe Shackney
Princeton Public Schools
Princeton Public Schools

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 05-30-2017 08:27 AM
Refer to my script Here

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 05-30-2017 08:58 AM
@gshackney there's no regexp issues in that snippet above unless something was lost or formatted from copying and pasting it into your Terminal.
fwiw you can try this as well, which is functionally the same
lpstat -v | awk '/HS-/{gsub(/:/,"");print $3}' | xargs -I{} lpadmin -x {}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 05-30-2017 09:35 AM
Not a "one line solution" but it works here :
#! /bin/bash
PRINTER_LIST=$(lpstat -v | grep "HS-")
PRINTER_LIST_COUNT=$(echo "${PRINTER_LIST}" | wc -l | bc)
for (( i = 1 ; i <= "${PRINTER_LIST_COUNT}" ; i++ )) ; do
CURRENT_PRINTER_NAME=$(echo "${PRINTER_LIST}" | tail -n $i | head -n 1 | awk -F "device for " '{ print $2 }' | awk -F ": " '{ print $1 }')
lpadmin -x "${CURRENT_PRINTER_NAME}"
done

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 05-30-2017 10:07 AM
@grepoli
You were correct, I had muffed the copy paste. Worked perfectly.
Actually looking at this I wonder if I can specify anything actually on the wrong print server. This is neat thanks all!
Gabe Shackney
Princeton Public Schools
Princeton Public Schools

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 05-30-2017 11:50 AM
@gshackney np, what do you mean re: specifying anything? also say hi to Pam in the English dept for me. thats my mom!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 05-31-2017 07:48 AM
So 1st of all,
I said hi and......
you need to call your mom more often...
2nd,
Can we grep what printer server the currently added printers are pointed to and only delete those? (However the printer server is not reflected in the name of the printer). The goal is we have an older printer server thats got "pk12" in the name and a newer printer server that has "hsps" in the name and we want to eliminate the printers that are added from the older server so we can have them re add the printers from the newer server using self service.
Gabe Shackney
Princeton Public Schools
Princeton Public Schools

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 06-01-2017 08:08 AM
@gshackney , mission accomplished.
for the second part, unfortunately I don't really have any experience with print servers, so really don't know how to pull that info with the cups command line tools. anything useful when you run lpstat -h server:port
? Other than that, I've got nothing
