Question about formatting results


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Question about formatting results
# 1  
Old 02-23-2005
Question about formatting results

OK, I have a command that is getting a result, that I am trying to format using awk.

I think it's pretty ugly, and there is probably a better way to do it, but this is sorta working for me. Anyway, the command:
Code:
cat /var/log/cups/page_log | grep testuser | grep My_office_printer | awk '{gsub ("\\[", ""); print $4}' | awk '{gsub (":", "/"); print}' | awk -F/ '{print $2, $1, $3}'

(FYI, this is just getting a list of all dates that testuser printed to My_office_printer. The unformatted line would look like this, so you can see why I have all those awks in there:

Code:
My_office_printer testuser 4 [09/Jun/2004:15:05:57 -0400] 2 1 - localhost

Will return this:
Code:
Jan 03 2005
Jan 03 2005
Jan 03 2005
Jan 03 2005
Jan 03 2005
Jan 03 2005
Jan 03 2005
Jan 03 2005
Jan 03 2005
Jan 05 2005
Jan 05 2005

I want to reduce this to one entry per date, showing the number of entries there were. For example:
Code:
Jan 03 2005 had 8 print jobs
Jan 05 2005 had 2 print jobs

Thanks in for everyone who has helped with all my noob questions
# 2  
Old 02-23-2005
I have n't modified much of your existing script,
but added one more pipe ...


Code:
grep testuser file2 | grep My_office_printer | awk '{gsub ("\\[", ""); print $4}' | awk '{gsub (":", "/"); print}' | awk -F/ '{print $2, $1, $3}' | awk '{ x=$0 ; arr[x]++ ; } END { for (x in arr ) print x,arr[x] } '

# 3  
Old 02-23-2005
....without commenting on the "relative" beauty and "terseness" of the code...
change the last piped awk to:
Code:
awk '{arr[$0]++ ; } END { for (x in arr ) print x " had " arr[x] " print jobs"} '

# 4  
Old 02-23-2005
Quote:
Originally Posted by vgersh99
....without commenting on the "relative" beauty and "terseness" of the code...
I'm going to take that as a compliment Smilie


Anyway, thanks, it your info serves my purpose.

Any idea if I can "sort" this my month?

Maybe you have some time to help "pretty up" my code?
# 5  
Old 02-23-2005
Quote:
Originally Posted by TheCrunge
I'm going to take that as a compliment Smilie


Anyway, thanks, it your info serves my purpose.

Any idea if I can "sort" this my month?
add yet another after the last awk: '|sort'

Quote:
Maybe you have some time to help "pretty up" my code?
not really - sorry. But one would get rid of all the grep-s AND 'merge' all the awk-s into one - consider this as a homework assignment Smilie
# 6  
Old 02-23-2005
Quote:
Originally Posted by vgersh99
not really - sorry. But one would get rid of all the grep-s AND 'merge' all the awk-s into one - consider this as a homework assignment Smilie
Hmmm... this project is actually a "homework" assignment of sorts already.

Anyway, you've been helpful and I appreciate it.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Formatting Question

Good Morning- I've been cloning a Solaris 9 SPARC machine using ufsrestoreand have successfully restored drive 0. Now I'm trying to set up the mirror drive. To label and format the new drive, I used#format -e ->select c0t1d0 ->label ->select SMI ->select no ->select no ->select yes and... (3 Replies)
Discussion started by: Stellaman1977
3 Replies

2. Shell Programming and Scripting

I want to add a variable for the results from the formula of one variable and results of another var

Good morning all, This is the file name in question OD_Orders_2019-02-19.csv I am trying to create a bash script to read into files with yesterdays date on the file name while retaining the rest of the files name. I would like for $y to equal, the name of the file with a formula output with... (2 Replies)
Discussion started by: Ibrahim A
2 Replies

3. Shell Programming and Scripting

GREP Formatting Question

I have the following nmap file output with the multiple IP’s listed in the format below. Is there a way that GREP can format the output to just display IP and any ports that contain 'http' for that IP on its own line? file: Host: 192.168.1.xxx () Ports: 80/open/tcp//http///,... (3 Replies)
Discussion started by: FCoda10
3 Replies

4. Shell Programming and Scripting

Can ctag and cscope support recording search results and displaying the history results ?

Hello , When using vim, can ctag and cscope support recording search results and displaying the history results ? Once I jump to one tag, I can use :tnext to jump to next tag, but how can I display the preview search result? (0 Replies)
Discussion started by: 915086731
0 Replies

5. UNIX for Dummies Questions & Answers

Data manipulation/ formatting question

How would I get this output to look $ cat newfile 13114 84652 84148 LIKE THIS?: 13114,84652,84148 sed,cut awk? syntax? (2 Replies)
Discussion started by: ddurden7
2 Replies

6. Shell Programming and Scripting

Question about formatting output using AWK

Hi everyone. I've got this fille... 0 5000001 5000002 5000003 5000006 5000007 5000219 11000156 11003130 11003132and this script... #!/bin/ksh FILE_ALERT_CONTACT_LIST=users.txt userID=`awk -F"=" '{printf $1 ", "}' $FILE_ALERT_CONTACT_LIST` (9 Replies)
Discussion started by: Fatbob
9 Replies

7. UNIX for Dummies Questions & Answers

Question about formatting the output

I need to ask a question on how to format the output in a csv format. Right now i am running a shell script which executes a command and the following output is append through a unix script in a .csv file. So the output of xyz.csv is as follow :- 1. Number = 25 Amount $84,132.22 2.... (1 Reply)
Discussion started by: chris1234
1 Replies

8. Shell Programming and Scripting

Text formatting question

How can i change the display of a text file containing 4980 167 187 4980 167 187 4980 167 180 4980 167 180 4980 167 179 4980 272 174 (7 Replies)
Discussion started by: aliaa2a
7 Replies

9. Shell Programming and Scripting

Multiple Grep Results - Formatting

Hello, Perhaps someone here can help with this. I'd like to grep a plain text file for a word and output each line containing a word found to a seperate line instead of back to back. Examples: Basic command: cat file.txt > grep -i CAT > results.txt file.txt: The cat said meow The... (7 Replies)
Discussion started by: sysera
7 Replies

10. UNIX for Dummies Questions & Answers

disk formatting question

Currently I have a box that I am dual-booting Win98 & Linux on. I have an unformatted 3 gig slice that I would like to install Soloris 8 x86 on. Are there any issues I should be aware of? How close is the x86 install to the sparc install? The Linux partition will be going away but I need to reatain... (1 Reply)
Discussion started by: 98_1LE
1 Replies
Login or Register to Ask a Question