Get a printer name into variable with similar printers

I am using a same printer vendor for 2 printer. Some of them dont have just default name with a full name of that printer.

for example

# lpstat -a | awk '{print $1}'|grep CITIZEN
CITIZEN
CITIZEN-T-S851II

This first one CITIZEN is a part of script

if grep -q S310II /etc/cups/printers.conf; then
PRINTER="$(lpstat -a | awk '{print $1}'|grep S310II)"

But this gave me nothing, so first lines are good, it find a S310II because of cups info, but NAME is not s310, just CITIZEN. So i Can change S310II to just CITIZEN, because you see first code i posted, what gave me, two CITIZEN.
How can I just get a printer name for that S310II. This script is helped me so much, but now with this similar names are a bit strange to get it.

I solve it like this

if grep -q S310II /etc/cups/printers.conf; then
 PRINTER="$(grep -B 5 CT-S310II /etc/cups/printers.conf | head -1 | cut -d "<" -f2 | cut -d ">" -f1 | awk -F " " '{print $2}')"

its a workaround but did the trick :smiley:

1 Like

Thanks for sharing.
What happens to PRINTER if the desired string is not found in the printers.conf ?

Your looong pipe with two grep and four more programs could be abbreviated to

PRINTER=$(awk -F"[< >]" '{TMP[NR%5]=$3} /Samsung ML-2250/ {print TMP[(NR+1)%5]; exit}'  /etc/cups/printers.conf)

May need some modification / adaption, as this is an approximation as I don't have your printer.conf around for testing.

I will test it. Thanks.

It gives me just true

 PRINTER=$(awk -F"[< >]" '{TMP[NR%5]=$3} /CT-S310II/ {print TMP[(NR+1)%5]; exit}'  /etc/cups/printers.conf)
# echo $PRINTER
true

Major problem is where printers.conf is different on different workstations. so maybe will be best to somehow search above <Printer> tag
So, not to find 5 or 4 lines above matched CT-S310II, instead do find above tag <printer>, maybe...

so this is a small portion of it. If you can see Printer is 4 lines below our matched text 350II. In some workostation is 4 below,. some 5, etc.
So maybe idea is to find <printer below it, and then with sed or awk get just BIXOLON. Some pseudocode maybe is: Find printer tag below matched string.

It's always helpful to show consistent, and sufficient, sample data. In your sample in post #9, I can't see "CiTIZEN" nor "S310II" matched as requested in post #1. The "<Printer BIXOLON>" line is two or four lines BEFORE a match of "35011", and it wouldn't be more specific that "CITIZEN", which, as I can see in post #1, wouldn't satisfy your request.
I you want a generic (taylored?) solution, please post

  • different input samples, i.e. different printers.conf
  • the specific data (line / field / composed string) you want from either
  • the match you want to supply for either search.

yeah, u are right. This is just a test. So what is matched string and name is irelevant. Point must be the same. Find the model, this is what is string check. When find, ok it is here, find printer name. So if I search just CITIZEN, and have similar printers, it comming confused. What is the point? I use printer for something else. So I use printer name. Very important to find model which I have and want to use. I have several printers usb so I don't use that. Good example is CT-S310II, so i want that, but nevermind that is irelevant as i said ealrier.
Point #2 is that when I find S3100 in printers.conf, find FIRST <printer tag, without that, only to leave a name, but that I will do with awk, just dont know how to find that first line <Printer below matched string S3100.