Omit repeating lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Omit repeating lines
# 1  
Old 02-22-2005
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....
# 2  
Old 02-22-2005
Code:
lpstat -W "completed" -o | awk -F- '{arr[$1]++}END {for (i in arr) printf("Printer->[%s] Jobs->[%d]\n", i, arr[i])'

# 3  
Old 02-22-2005
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!
# 4  
Old 02-22-2005
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!
# 5  
Old 02-22-2005
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?
# 6  
Old 02-22-2005
OK, here's the deal (my head is starting to hurt here)Smilie

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 04:21 PM..
# 7  
Old 02-22-2005
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"
  }
}

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash repeating lines for some files but not all

The bash below executes and seems to work fine on those files in which . However on those files where there is no additional CNV detected that line repeats multiple times instead of only once. I tried adding an END as all lines are printed but that doesn't help. I can not seem to solve this... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Deleting Repeating lines from a txt file via script

Hi, I'm having trouble in achieving the following scenario. There is a txt file with thousands of lines and few lines are repeated, which needs to be removed using a script. File.txt 20140522121432,0,12,ram Loc=India From=ram@xxx.com, To=ravi@yyy.com,, 1 2 3 4 . . 30... (18 Replies)
Discussion started by: Gautham
18 Replies

3. UNIX for Dummies Questions & Answers

Last line omit

I have a csv file "file.csv" which has 5 records but when I check record count for file.csv in unix it shows as 6, the last line is empty and I need to omit and need for processing. Kindly help. Thanks (6 Replies)
Discussion started by: Prashanth B
6 Replies

4. Shell Programming and Scripting

Compare last 90 logs and print repeating lines with >20

*log files are in date order sample logs... ciscoresets_20120314 ciscoresets_20120313 ciscoresets_20120312 ciscoresets_20120311 ciscoresets_20120310 cat ciscoresets_20120314 SYDGRE04,10,9 SYDGRE04,10,10 SYDGRE04,10,11 SYDGRE04,10,12 SYDGRE04,10,13 SYDGRE04,10,14 SYDGRE04,10,15... (2 Replies)
Discussion started by: slashbash
2 Replies

5. Shell Programming and Scripting

Printing the lines which are repeating in a files

Hi, I need to find the lines which are repeating in a file cat file1 abcdef 23-1 abcdef 24-1 bcdeff 25-0 ttdcfg 26-0 ttdcfg 20-0 bcdef1 25-0 bcdef2 25-0 bcdef3 25-0 bcdef4 25-0 bcdef4 00-0any help is greatly appreciated. Thanks in advance. In need to find which one are... (3 Replies)
Discussion started by: jpkumar10
3 Replies

6. Shell Programming and Scripting

Removing repeating lines from a data frame (AWK)

Hey Guys! I have written a code which combines lots of files into one big file(.csv). However, each of the original files had headers on the first line, and now that I've combined the files the headers are interspersed throughout the new combined data frame. For example, throughout the data... (21 Replies)
Discussion started by: gd9629
21 Replies

7. UNIX for Dummies Questions & Answers

Remove groups of repeating lines

I know uniq exists, but am not sure how to remove repeating lines when they are groups of two different lines repeating themselves, without using sort. I need them to be sorted in the original order, just to remove repeats. cd /media/AUDIO/WAVE/9780743518673/mp3 ~/Desktop/mp3-to-m4b... (1 Reply)
Discussion started by: glev2005
1 Replies

8. Shell Programming and Scripting

Merging non-repeating columns of lines

Hello, I have file to work with. It has 5 columns. The first three, altogether, constitutes the position. The 4th column contains some values for downstream analysis and the fifth column contains some values that I want to add to 4th column (only if they happen to be in the same position). My... (5 Replies)
Discussion started by: menenuh
5 Replies

9. Shell Programming and Scripting

Omit Blank Lines while comparing two files.

Hello All, I am writting file comparison Utility and I have encountered such a senario where there are 2 files such as follows- 1#!/usr/local/bin/python 2 import gsd.scripts.admin.control.gsdPageEscalate 3.gsd.scripts.admin.control.gsdPageEscalate.main() 1 #!/usr/local/bin/python... (10 Replies)
Discussion started by: Veenak15
10 Replies

10. Shell Programming and Scripting

merge 2 files (without repeating any lines)

I need to add the content of file1 to file2 - all lines but not those existing in file2 already, so the "cat file1 >> file2" doesn't work. For example, file1: 100 xxxxxx str1 102 xxxxxx str2 File2: 50 xxxxxxx xxx 30 xxxxxxxxxxx 102 xxxxxx str2 xxxx ...... the result: 50 xxxxxxx... (9 Replies)
Discussion started by: bluemoon1
9 Replies
Login or Register to Ask a Question