Bash: executing a command based on organized output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: executing a command based on organized output
# 1  
Old 04-05-2011
Bash: executing a command based on organized output

Just learning scripting. I need to remove duplicate managed printers using lpadmin. I have the following script (it's rough and probably a better way to do it) that returns the values as IP (column 1) Printer Name (column 2).
command:
lpstat -v | grep -E '192.168.*.20|192.168.*.21|192.168.*.22|192.168.*.23|192.168.*.24|192.168.*.25|192.168.*.90' | awk '{print $4,$3"/"}' | sort | awk -F "/" '{print $3,$4}'

output:
192.168.10.20 MX4100N:
192.168.24.21 AR208D:
192.168.24.22 mcx_5:
192.168.24.22 mcx_8:
192.168.24.22 M620N:
192.168.24.23 mcx_6:
192.168.24.23 M363N1:
192.168.24.24 mcx_7:
192.168.24.24 M363N2:
192.168.24.25 M363N3:
192.168.24.90 mcx_0:
192.168.24.90 mcx_1:
192.168.24.90 mcx_2:
192.168.24.90 mcx_3:
192.168.24.90 mcx_4:
What I would like to do is set up a loop that tells lpadmin to remove duplicate IP based printers based on printer name. I want to keep the highest value where they are mcx_* (managed printer) and if there is a named printer with the same IP keep the mcx printer. So from the above output I want to send the following commands to clean up the machine.
lpadmin -x M620N
lpadmin -x M363N1
lpadmin -x M363N2
lpadmin -x mcx_0
lpadmin -x mcx_1
lpadmin -x mcx_2
lpadmin -x mcx_3
lpadmin -x mcx_5
This would result in the following list of installed printers.
192.168.10.20 MX4100N:
192.168.24.21 AR208D:
192.168.24.22 mcx_8:
192.168.24.23 mcx_6:
192.168.24.24 mcx_7:
192.168.24.25 M363N3:
192.168.24.90 mcx_4:
I was thinking of assigning the output to an array and using a loop but can't wrap my brain around how to accomplish this. Does this make sense? I think I'm confusing myself trying to explain it!
# 2  
Old 04-05-2011
Code:
sort -r |awk '{if(++a[$1]==1){print >"installed_printers"}else {print "Ipadmin -x "$2 > "clean_up"}}'

This User Gave Thanks to yinyuemi For This Post:
# 3  
Old 04-05-2011
Thank you yinyuemi! So output goes to the files and then I can chmod +x "clean_up" and run it. This is exactly what I needed to do. A few questions in follow up.
Can you break down your code or point me to somewhere I can look to get an explanation? I have been perusing forums and how-to's and googling and had no luck. I was obviously looking for the wrong thing.

Could this be done without creating files? It's not a huge deal. I can add a few lines for clean-up at the end of the script to remove these. It's really more of a "can it be done" question.
Thanks again!
# 4  
Old 04-05-2011
first code "
Code:
sort -r

"
Code:
echo "192.168.10.20 MX4100N:
192.168.24.21 AR208D:
192.168.24.22 mcx_5:
192.168.24.22 mcx_8:
192.168.24.22 M620N:
192.168.24.23 mcx_6:
192.168.24.23 M363N1:
192.168.24.24 mcx_7:
192.168.24.24 M363N2:
192.168.24.25 M363N3:
192.168.24.90 mcx_0:
192.168.24.90 mcx_1:
192.168.24.90 mcx_2:
192.168.24.90 mcx_3:
192.168.24.90 mcx_4:" |sort -r
192.168.24.90 mcx_4:
192.168.24.90 mcx_3:
192.168.24.90 mcx_2:
192.168.24.90 mcx_1:
192.168.24.90 mcx_0:
192.168.24.25 M363N3:
192.168.24.24 mcx_7:
192.168.24.24 M363N2:
192.168.24.23 mcx_6:
192.168.24.23 M363N1:
192.168.24.22 mcx_8:
192.168.24.22 mcx_5:
192.168.24.22 M620N:
192.168.24.21 AR208D:
192.168.10.20 MX4100N:

After sorted reversely, you can see the red lines has the "highest value", also it is the first one if the IP has duplicated other ones. However, it might only worked for your exampled data, since the data of sec. column was sorted based on the ALPHA characters, that means if the first letter after "M", like N,O..., the first one of one certain IP would not be "max..", so the results could not be correct.

The awk code:
Code:
awk '{
if(++a[$1]==1) ## ++a[$1]==1 means the first time of apprearance of $1, a[$1] is a array.
{print >"installed_printers"} ## they're printed as "installed_printers" file
else 
{print "Ipadmin -x "$2 > "clean_up"} ## else, printed as "clean_up" file
}'

if any problem, let me know. Sorry for my bad englishSmilie
This User Gave Thanks to yinyuemi For This Post:
# 5  
Old 04-05-2011
Awesome job explaining so not to worry about your english. This helps greatly and will come in handy for future projects I'm sure. Thank you so much for your help. The task of dealing with duplicate printers just got a whole lot easier.
# 6  
Old 04-05-2011
You could also replace the egrep and 2 awks above with this:

Code:
lpstat -v | awk ' $4 ~ /^192\.168\.[0-9]+\.(2[0-5]|90)$/ { print $4,$3 }' | sort -r | ...yinyuemi's awk script...

if field 4 matches <begining of string> 192 <dot> 168 <dot> <one-or-more-digits> <dot> <either "2<zero thru 5>" or "90"> <end of string>
then print field 4 <space> field 3

Last edited by Chubler_XL; 04-05-2011 at 08:48 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 04-06-2011
Thanks Chubler_XL. That shortens things up. I figured there was an easier/better way to get what I needed. I'm using a lot of pipes until I figure out the proper way to do things. I'm getting a lot of good information from this site and the folks here.
Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing sed command inside a bash script

I want to run commands inside a bash script. An example is I want to pass the command in a string as regexp as an argument to the script, then run sed on the bash variable sed.sh regexp sed.sh "-i \"s/<p>//g\"" then call sed "$regexp" $fl (3 Replies)
Discussion started by: Kangol
3 Replies

2. Shell Programming and Scripting

Use bash command on awk field and output the result

Hello, I want to run a field from an awk command through a command in bash. For example my input file is 1,2,3 20,30,40 60,70,80 I want tot run $2 thought the command date +%d/%m/%y -d"01/01/15 + $2 days -1 day" and get the output 1,02/01/15,3 20,30/01/15,40 60,11/03/15,80 ... (2 Replies)
Discussion started by: garethsays
2 Replies

3. UNIX for Dummies Questions & Answers

Write pid and command name to a txt file while executing a bash script

Hi All, Just have a requirement, I am executing a bash shell script, my requirement is to catch the pid and job name to a txt file in the same directory, is there anyway to do it? please help me out. Regards Rahul ---------- Post updated at 08:42 AM ---------- Previous update was at... (2 Replies)
Discussion started by: rahulkalra9
2 Replies

4. Shell Programming and Scripting

Bash - command is executing but do nothing without complaining

Hello. This part of script do nothing but no complain. Here the directory content : The script run by root cd /root CMD="rmdir -p -v --ignore-fail-on-non-empty .ssh" echo $CMD $CMD The ouput linux:~/bin # ./test_05 rmdir -p -v --ignore-fail-on-non-empty .ssh rmdir: removing directory,... (3 Replies)
Discussion started by: jcdole
3 Replies

5. Shell Programming and Scripting

Read 2 lines from File, Run Command based off output

Okay, so I have a file containing line after line of three digit numbers. I need a script that does an action based on the last two numbers in this list. So.... To get the last two numbers, I can have the script do tail -2 filename.txt But where I run into trouble is as follows. If... (6 Replies)
Discussion started by: UCCCC
6 Replies

6. Shell Programming and Scripting

Dynamic case creation based on output list from a command

I am attempting to create a script that would allow me to list all the instances associated with a DB2 and then prompt the user to choose which one to issue the db2profile command against. I use the db2 command db2ilist to get a list of the instances for a particular server, but the number of... (7 Replies)
Discussion started by: slatoms
7 Replies

7. Red Hat

Dynamic case creation based on output list from a command

I am attempting to create a script that would allow me to list all the instances associated with a DB2 and then prompt the user to choose which one to issue the db2profile command against. I use the db2 command db2ilist to get a list of the instances for a particular server, but the number of... (1 Reply)
Discussion started by: slatoms
1 Replies

8. Shell Programming and Scripting

Piping output from a command into bash script

Hi all. I am using procmail to deliver an email to a script I am developing. Procmail delivers the email to the script on standard input. I imagine this is the same as piping input from a command into the script. Hence I've been testing my script by running echo 'test' | sms-autosend-backup.sh ... (2 Replies)
Discussion started by: akindo
2 Replies

9. UNIX for Dummies Questions & Answers

How do i tell my bash shell script to test the output of a command against something

How do i tell my bash shell script to test the output of the command i'm using?? I want this script to look for lines not equal to 1 then let me know.. $ cat blah ; echo ---- ; cat blah.sh 1 fe 1 fi 1 fo 0 fum 1 blahda 1 blah 0 blahh 1 bla 1 bl 1 blahhh ---- #!/bin/bash while... (1 Reply)
Discussion started by: phpfreak
1 Replies

10. Shell Programming and Scripting

processing tab-formated output of command w/bash

I have a command that when ran it will have an output such as string LongerString string2 longerString2 More MoreStrings seperated by tabs. The command lists domains and their accounts set up in my server admin software (interworx). The end result will be that it will run rsync for... (2 Replies)
Discussion started by: sweede
2 Replies
Login or Register to Ask a Question