The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using cp -r command to selectively omit *.dat files while copying a directory. d_sai_kumar UNIX for Dummies Questions & Answers 5 07-18-2008 03:48 AM
need assistance: sed and repeating patterns metalwarrior UNIX for Advanced & Expert Users 1 02-02-2008 02:00 AM
Loop through files in dir, omit file with latest date stringzz Shell Programming and Scripting 2 12-04-2007 11:04 AM
merge 2 files (without repeating any lines) bluemoon1 Shell Programming and Scripting 9 10-25-2007 07:31 PM
Repeating commands in a script Dave2874 Shell Programming and Scripting 4 03-14-2005 07:34 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-22-2005
Registered User
 

Join Date: Feb 2005
Posts: 30
Omit repeating lines

Can someone help me with the following 2 objectives?

1) The following command is just an example. It gets a list of all print jobs. From there I am trying to extract the printer name. It works with the following command:

Code:
lpstat -W "completed" -o | awk -F- '{ print $1}'
Problem is, I want to be able to omit these multiple lines:

Code:
My_office_printer
My_office_printer
My_office_printer
My_home_printer
My_home_printer
So it looks like this:

Code:
My_office_printer
My_home_printer
Now, each line is actually different (because each one is its own print job), but because I use awk to just get the name of the printer, the end result displays the printer name for however many print jobs there were. I just want one instance of each printer name.


2) In the previous section, I want to reduce the number of lines returned to one per printer. Before I do, I want to count how many there are. For example, how many time was My_office_printer listed.

Any help is greatly appreciated....
Reply With Quote
Forum Sponsor
  #2  
Old 02-22-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,019
Code:
lpstat -W "completed" -o | awk -F- '{arr[$1]++}END {for (i in arr) printf("Printer->[%s] Jobs->[%d]\n", i, arr[i])'
Reply With Quote
  #3  
Old 02-22-2005
Registered User
 

Join Date: Feb 2005
Posts: 30
Holy moly, thanks a heap. That works great!!

I'll be back with more questions

For example, I need to make a graphical representation of that. That's a whole other can of worms though.

Thanks again!
Reply With Quote
  #4  
Old 02-22-2005
Registered User
 

Join Date: Feb 2005
Posts: 30
OK, my next question on this:

Code:
lpstat -W "completed" -o | grep "testguy" | awk -F- '{arr[$1]++}END {for (i in arr) printf("Printer: %s Jobs: %d\n", i, arr[i])}'
How would I get the user name in the result now that I'm filtering by users?

For example:

Code:
Printer: My_office_printer
Jobs: 21
User: testguy
Again, thanks!
Reply With Quote
  #5  
Old 02-22-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,019
Quote:
Originally Posted by TheCrunge
OK, my next question on this:

Code:
lpstat -W "completed" -o | grep "testguy" | awk -F- '{arr[$1]++}END {for (i in arr) printf("Printer: %s Jobs: %d\n", i, arr[i])}'
How would I get the user name in the result now that I'm filtering by users?

For example:

Code:
Printer: My_office_printer
Jobs: 21
User: testguy
Again, thanks!
what is the sample output you're trying to parse? pls post a sample with multiple users as it will need to be parsed.

Single line OR multiple lines per user?
Reply With Quote
  #6  
Old 02-22-2005
Registered User
 

Join Date: Feb 2005
Posts: 30
OK, here's the deal (my head is starting to hurt here)

I need to get the number of Jobs that are printed on each printer, per date, by user.

What you did earlier helped get the printer name and number of jobs. I tried to change it a little, so I can get some more info (the date) out of it.

So, if I enter "testguy" as my user (variable from other script), then I want the following returned:

Code:
Tue Feb 22: My_office_printer: 3 Jobs - My_home_printer: 5 Jobs"
Mon Feb 21: My_office_printer: 2 Jobs - My_home_printer: 8 Jobs"
Fri Feb 18: Someone_elses_printer: 9 Jobs
etc....
I will then need to be able to use each line as a variable so I can find a way to "graph" this later....

I'll try to explain it better, but I thought I would post this anyway...


In case it helps, here is all the info returned by
Code:
 lpstat -W "completed" -o | grep "testguy"
-W "completed" show all jobs that have been completed, not waiting to complete. The -o will show for all printers. There is a -u flag, but grepping the user seems to work better.

Code:
My_office_printer-494       testguy           23552   Fri Feb 18 09:54:42 2005
This is one job entry. The actual result is many lines, all with this format.
I need to strip the unique job number from the name of the printer (different for each entry, in this case it's -494). The 23552 is the number of bytes the job is, don't need that, and I don't need the time and year. So, really, what I need here is $1,$2,$4,$5,$6, and $1 has to be without the -494 or whatever the unique job number is).

Last edited by TheCrunge; 02-22-2005 at 01:21 PM.
Reply With Quote
  #7  
Old 02-22-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,019
how about something like that for starters:

lpstat -W "completed" -o | grep "testguy" | nawk -f crunge.awk

here's crunge.awk:
Code:
BEGIN{
  FLDprt="1"
  FLDusr="2"
}

{
  printer=substr($FLDprt,1,index($0,"-")-1)
  date=$(FLDusr+2) " " $(FLDusr+3) " "$(FLDusr+4)
  dates[date]
  arr[date, printer]++;
}
END {
  for (dateI in dates) {
    printf("%s:", dateI);
    for (dataI in arr) {
      split(dataI, dataIa, SUBSEP)
      if (dataIa[1] == dateI) {
           printf(" %s: %d Jobs - ",
                dataIa[2],  arr[dataI])
           delete arr[dataI];
      }
    }
    print "\"\n"
  }
}
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0