Get a printer name into variable with similar printers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get a printer name into variable with similar printers
# 1  
Old 02-16-2019
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
Code:
# lpstat -a | awk '{print $1}'|grep CITIZEN
CITIZEN
CITIZEN-T-S851II

This first one CITIZEN is a part of script
Code:
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.
# 2  
Old 02-16-2019
I solve it like this

Code:
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 Smilie


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-16-2019 at 02:48 PM.. Reason: Changed QUOTE to CODE tags.
This User Gave Thanks to tomislav91 For This Post:
# 3  
Old 02-16-2019
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
Code:
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.
# 4  
Old 02-16-2019
Quote:
Originally Posted by 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
Code:
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.
# 5  
Old 02-16-2019
It gives me just true
Code:
 PRINTER=$(awk -F"[< >]" '{TMP[NR%5]=$3} /CT-S310II/ {print TMP[(NR+1)%5]; exit}'  /etc/cups/printers.conf)
# echo $PRINTER
true

# 6  
Old 02-17-2019
Quote:
Originally Posted by RudiC
...

May need some modification / adaption, as this is an approximation as I don't have your printer.conf around for testing.
# 7  
Old 02-20-2019
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To find ls of similar pattern files in a directory by passing the variable.

Hi, I have file in my $datadir as below :- SAT_1.txt SAT_2.txt BAT_UD.lst BAT_DD1.lst DUTT_1.txt DUTT_la.txt Expected result :- should get all the above file in $<Filename>_file.lst Below is my code :- for i in SAT BAT DUTT do touch a.lst cd $datadir (1 Reply)
Discussion started by: satishmallidi
1 Replies

2. Shell Programming and Scripting

Perl match multiple numbers from a variable similar to egrep

I want to match the number exactly from the variable which has multiple numbers seperated by pipe symbol similar to search in egrep.below is the code which i tried #!/usr/bin/perl my $searchnum = $ARGV; my $num = "148|1|0|256"; print $num; if ($searchnum =~ /$num/) { print "found"; }... (2 Replies)
Discussion started by: kar_333
2 Replies

3. Linux

Find printer location and printer type

Hi, Is it possible to find the printer location and printer type (whether it is local or network) using command in Linux ? Thanks in advance. (1 Reply)
Discussion started by: forumguest
1 Replies

4. Shell Programming and Scripting

Using sed (or similar) to rename variable headings

Hello, I'm rather new to the world of regular expressions and sed, though am excited by its possibilities. I have a particular task I'd like to achieve, and have googled the topic quite a bit. However, having found some codes that perform a task very similar to what I'd like to do, I can't for... (2 Replies)
Discussion started by: redseventyseven
2 Replies

5. AIX

Check printer queue on Windows printer server

Hello Let me first give a small overview of the setup. All printers are connected to Windows 2000 servers. There are a lot of UNIX (AIX & HP-UX) servers as well which have SAP running. I'm working on a script to add printers to a specified SAP instance. I want to verify the user input (to... (0 Replies)
Discussion started by: NielsV
0 Replies

6. Solaris

Configuring Printer with Printer Manager

Hi All, I am trying to configure printer in solaris 10 with the help of print manager. There is no printer attached to my system, ia m doing it for test purpose. However I am unable to do so coz its pops up window - Heading as error with option as dismiss and cancel. Kindly help as I am... (3 Replies)
Discussion started by: kumarmani
3 Replies

7. Linux

know I do for to printer in printer deskjet 80colun

I want to print some thing in HP Deskjet 692.? (1 Reply)
Discussion started by: edvaldo
1 Replies

8. UNIX for Dummies Questions & Answers

Printers

I'm in need of finding out on how to mount and dismount printers. Thx, shawnnee (1 Reply)
Discussion started by: shawnnee
1 Replies
Login or Register to Ask a Question