# Get a printer name into variable with similar printers

**URL:** <https://community.unix.com/t/get-a-printer-name-into-variable-with-similar-printers/374889>\
**Category:** Shell Programming and Scripting\
**Created:** [February 16, 2019, 9:38am UTC](https://community.unix.com/t/get-a-printer-name-into-variable-with-similar-printers/374889 "2019-02-16T09:38:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomislav91](https://community.unix.com/letter_avatar/tomislav91/32/6_449c533f586939b6e5f246632a46421f.png) [@tomislav91](https://community.unix.com/u/tomislav91)\
**Post date:** [February 16, 2019, 9:38am UTC](https://community.unix.com/t/get-a-printer-name-into-variable-with-similar-printers/374889/1 "2019-02-16T09:38:04Z")

</div>

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

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

```

This first one CITIZEN is a part of script

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![tomislav91](https://community.unix.com/letter_avatar/tomislav91/32/6_449c533f586939b6e5f246632a46421f.png) [@tomislav91](https://community.unix.com/u/tomislav91)\
**Post date:** [February 16, 2019, 12:16pm UTC](https://community.unix.com/t/get-a-printer-name-into-variable-with-similar-printers/374889/2 "2019-02-16T12:16:28Z")

</div>

I solve it like this

```nohighlight
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 😃

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 16, 2019, 2:10pm UTC](https://community.unix.com/t/get-a-printer-name-into-variable-with-similar-printers/374889/3 "2019-02-16T14:10:41Z")

</div>

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

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![tomislav91](https://community.unix.com/letter_avatar/tomislav91/32/6_449c533f586939b6e5f246632a46421f.png) [@tomislav91](https://community.unix.com/u/tomislav91)\
**Post date:** [February 16, 2019, 2:13pm UTC](https://community.unix.com/t/get-a-printer-name-into-variable-with-similar-printers/374889/4 "2019-02-16T14:13:37Z")

</div>

> [@rudic](#):
>
> 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
> 
> ```plaintext
> 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.

---

<div class="post-metadata">

**Author:** ![tomislav91](https://community.unix.com/letter_avatar/tomislav91/32/6_449c533f586939b6e5f246632a46421f.png) [@tomislav91](https://community.unix.com/u/tomislav91)\
**Post date:** [February 16, 2019, 3:50pm UTC](https://community.unix.com/t/get-a-printer-name-into-variable-with-similar-printers/374889/5 "2019-02-16T15:50:41Z")

</div>

It gives me just true

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

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 17, 2019, 4:55am UTC](https://community.unix.com/t/get-a-printer-name-into-variable-with-similar-printers/374889/6 "2019-02-17T04:55:19Z")

</div>

> [@rudic](#):
>
> ...
> 
> May need some modification / adaption, as this is an approximation as I don't have your `printer.conf` around for testing.

---

<div class="post-metadata">

**Author:** ![tomislav91](https://community.unix.com/letter_avatar/tomislav91/32/6_449c533f586939b6e5f246632a46421f.png) [@tomislav91](https://community.unix.com/u/tomislav91)\
**Post date:** [February 20, 2019, 4:06am UTC](https://community.unix.com/t/get-a-printer-name-into-variable-with-similar-printers/374889/7 "2019-02-20T04:06:37Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 20, 2019, 5:46am UTC](https://community.unix.com/t/get-a-printer-name-into-variable-with-similar-printers/374889/8 "2019-02-20T05:46:22Z")

</div>

> [@rudic](#):
>
> ... I don't have your `printer.conf` around for testing.

---

<div class="post-metadata">

**Author:** ![tomislav91](https://community.unix.com/letter_avatar/tomislav91/32/6_449c533f586939b6e5f246632a46421f.png) [@tomislav91](https://community.unix.com/u/tomislav91)\
**Post date:** [February 20, 2019, 5:58am UTC](https://community.unix.com/t/get-a-printer-name-into-variable-with-similar-printers/374889/9 "2019-02-20T05:58:49Z")

</div>

> [@](#):
>
> \<Printer BIXOLON\>  
> UUID urn:uuid:6b4abb5d-c3b3-3eee-4923-a6128aefe38a  
> Info BIXOLON SRP-350II  
> Location test1  
> DeviceURI usb://BIXOLON/SRP-350II?serial=00000001

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.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 20, 2019, 6:39am UTC](https://community.unix.com/t/get-a-printer-name-into-variable-with-similar-printers/374889/10 "2019-02-20T06:39:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tomislav91](https://community.unix.com/letter_avatar/tomislav91/32/6_449c533f586939b6e5f246632a46421f.png) [@tomislav91](https://community.unix.com/u/tomislav91)\
**Post date:** [February 20, 2019, 6:41am UTC](https://community.unix.com/t/get-a-printer-name-into-variable-with-similar-printers/374889/11 "2019-02-20T06:41:35Z")

</div>

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.
