Awk output issues.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk output issues.
# 1  
Old 05-16-2008
Awk output issues.

I have the follwing code:

Code:
awk '{print $1}' HITS                                   #Searches HITS file column one. Column one is filenames

awk '{print $2}' HITS  | sort -n | wc -l            #Searches HITS file and sorts numerically and outputs line count. column 2 is IP addresses

awk '{print $2}' HITS | uniq | wc -l                # Searches HITS file for unique entries and outputs line count. column 2 is IP addresses

i know my code is not right and my results are not listed how i want it. they are just displayed one after the other on seperate lines.

like this:
Code:
hits/adverts.hits:248.204.125.183
hits/mags.hits:87.114.172.31
hits/adverts.hits:34.220.19.30
hits/food.hits:185.227.145.86
hits/food.hits:213.225.8.140
hits/mags.hits:83.222.98.178
hits/food.hits:118.195.119.35
10345
245

What I want them to all be on the same line in a table to look like this:

Code:
FILENAME                    HITS                       UNIQUE HITS
food.hits                   2034                            245
mags.hits                   2000                            435
adverts.hits                1456                            344

#the hits column needs to also be in a descending order as shown above


Last edited by amatuer_lee_3; 05-16-2008 at 05:04 AM..
# 2  
Old 05-16-2008
may be you should provide input data which gives clarity on your requirement.

-ilan
# 3  
Old 05-16-2008
There wont be any input data i just want the function to display the results.
# 4  
Old 05-16-2008
Did you know it's spelt 'amateur'?

Some comments regarding your existing code:

Code:
awk '{print $1}' HITS

# you need to specify the column separator because awk uses 
# white space (spaces/tabs) by default , e.g.

awk -F: '{print $1}' HITS

Code:
awk '{print $2}' HITS  | sort -n | wc -l

# no need for awk and sort, as it doesn't change the number of lines of data, just:

wc -l < HITS

Code:
 awk '{print $2}' HITS | uniq | wc -l

# if the data is unsorted uniq does not identify matching lines, better to use:

awk -F: '{print $2}' HITS | sort -u | wc -l

Personally I would use one awk script to generate all of the results, something like:

Code:
sort -t : -k 1,1 -k 2,2 HITS | awk -F: '
        # assign values to variables for readability, count a hit
        { file=$1; ip=$2; hits[file]++ }
        # initialise prevfile when reading the first line
        NR==1 { prevfile=file }
        # if it is a new file, reset the previous IP
        file != prevfile { previp="" }
        # if the ip is different to the previous IP, count a unique hit
        ip != previp { uniquehits[file]++ }
        # save previous ip and file name for future reference
        { previp=ip; prevfile=file }
        # output the results
        END { for (file in hits) { print file,hits[file],uniquehits[file] } }
'

This won't output in exactly the format you wanted, you can use printf() for that, but I'll leave that part as an exercise for you!
# 5  
Old 05-16-2008
Thanks very much. Yeah i do know its spelt wrong. I noticed after i clicked confirm on my username. just cant be bothered to change it.

can i refer you to my recent post:

https://www.unix.com/shell-programmin...-uniq-awk.html

This gives a better explanation as to what problems i have. This is kind of irrelevant now because i misinterpreted my requirements. But this still does help me. 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

Issues formatting output of two commands in a single line.

I wish to generate output of two commands in the same line separated by a single white-space. Below is my command and output in the same line. ls -ltr fname1.out | awk '{$2=$4=$5=x; print}' | tr '\n' '\t' | tr -s ' '; cksum<fname1.out | cut -d' ' -f1 Output: -rw-r--r--. root Aug 26 16:57... (6 Replies)
Discussion started by: mohtashims
6 Replies

2. Shell Programming and Scripting

Command output redirection to file issues

Hi, I have a peculiar issue w.r.t redirecting the command output to a file when using loop. I am redirecting command output to same file in a series of if condition statements, but if one block of if condition statement writes the log to the file , the subsequent block of if condition... (7 Replies)
Discussion started by: ananan
7 Replies

3. Shell Programming and Scripting

Email Format Output issues

Hi Guys, I have written a script, which output information from email notfication. The output works fine in HTML format, but non-html format it is not shown in a human readable format. Can you help with the format *** Script echo " Server Name : $CLIENT_CHECK "... (4 Replies)
Discussion started by: Junes
4 Replies

4. Shell Programming and Scripting

Issues with executing awk

I am piping some output to awk and would like to print fields $1 $2 and $3 $4 only if they exist. Note the awk begins with awk '{print $NF " " since I want the last field printed first. (7 Replies)
Discussion started by: motdman
7 Replies

5. Shell Programming and Scripting

Issues in reading file using 'awk'

Dear all, I am using following function of some script to assign variable "JobNo" some value form file $SAMPLE"_status.log" ( generated using the red color command ) crab ntuplize_crab -status -c $SAMPLE >& $SAMPLE"_status.log" & echo $SAMPLE"_status.log" "=====" jobNo=$(awk... (10 Replies)
Discussion started by: emily
10 Replies

6. Shell Programming and Scripting

Issues with awk and tcsh

Hello experts, I have two files which I'm uploading. One is an awk script and other file acts as an input to the script via positional parameter. awk -f intlmenu.awk jobsq.txt This run fine in C shell on SCO OpenServer Release 5.0.7. When I run it on Solaris 10 ( tcsh shell ) I get... (2 Replies)
Discussion started by: maverick_here
2 Replies

7. Shell Programming and Scripting

Awk OFS issues

Hi, Could anyone tell me what Im doing wrong here any help will be much appreciated #!/bin/bash ls -ltr /export/home/tjmoore > /export/home/tjmoore/log100 awk -F " " /export/home/tjmoore/log100 'BEGIN {OFS="\t";} {print $1,$2,$3,$4,$5, $6,$7,$8,$9;}' > /export/home/tjmoore/log1001 I... (9 Replies)
Discussion started by: 02JayJay02
9 Replies

8. Shell Programming and Scripting

Awk OFS issues

Hi Im trying to tidy up the output of a who command when it writes to a log, everything I've tried doesnt seem to work though, any help would be massively appreciated. Im using the awk command to set the OFS as tab. #!/bin/bash who >> /export/home/tjmoore/logusers awk -F 'BEGIN... (3 Replies)
Discussion started by: 02JayJay02
3 Replies

9. UNIX for Dummies Questions & Answers

Awk Performance Issues

Hi All, I'm facing an issue in my awk script. The script is processing a large text file having the details of a number of persons, each person's details being written from 100 to 250 tags as given below: 100 START| 101klklk| ... 245 opr| 246 55| 250 END| 100 START| ... 245 pp| 246... (4 Replies)
Discussion started by: pgp_acc1
4 Replies

10. AIX

Issues with AWK

Hi there I have written a ksh script on a Red Hat OS and the following extract works. awk '{if (NR != 1) {print $rec1_field }}' $file1 >> combined When I run the same script on an AIX OS, I get the following error. awk: 0602-562 Field $() is not correct. The input line number is 2. The... (12 Replies)
Discussion started by: alanp
12 Replies
Login or Register to Ask a Question