awk idea


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk idea
# 1  
Old 06-17-2011
awk idea

Hello everybody,

I am new here as well as in scripting.
I need some help. I have log files that are boring to examine. I wanted to use awk to do this:

-get the lines that match the eight field (user mac address)
-now sort them according to date (first 2 fields)
-print the following:

"number of repetition" "Date" "my text about user "

I started with this:
Code:
awk '{if ($8~"user mac address") printf("my text\n")}' log_file

and i got my text for eg. 5 times because this users mac appears 5 times in log:
Code:
my text
my text
my text
my text
my text

what is the best way to get this:
Code:
3 Jun 17 my text 
2 Jun 16 my text

thanx in advance

Last edited by Franklin52; 06-17-2011 at 08:34 AM.. Reason: Please use code tags
# 2  
Old 06-17-2011
maybe you want to use an array i.e a[yourMacAddr]="value you want"
or you paste an example including input and outputs.
# 3  
Old 06-17-2011
@sk1418, thanx for replying.

this is the shorten example of log lines:
Code:
Jun 16 07:12:11 dhcpd: DHCPDISCOVER from "usermac1"
Jun 16 09:42:22 dhcpd: DHCPDISCOVER from "usermac1"
Jun 16 11:45:55 dhcpd: DHCPDISCOVER from "usermac1"
 
Jun 17 08:47:29 dhcpd: DHCPDISCOVER from "usermac2"
Jun 17 10:49:11 dhcpd: DHCPDISCOVER from "usermac2"
Jun 17 12:38:15 dhcpd: DHCPDISCOVER from "usermac1"

i want the output to look like this :
Code:
Jun 16 user1 3
Jun 17 user2 2
Jun 17 user1 1

p.s. I dont have much users so i could corelate their mac addresses with names

Last edited by Franklin52; 06-17-2011 at 08:34 AM.. Reason: Please use code tags
# 4  
Old 06-17-2011
Code:
% cat testfile
Jun 16 07:12:11 dhcpd: DHCPDISCOVER from "usermac1"
Jun 16 09:42:22 dhcpd: DHCPDISCOVER from "usermac1"
Jun 16 11:45:55 dhcpd: DHCPDISCOVER from "usermac1"
 
Jun 17 08:47:29 dhcpd: DHCPDISCOVER from "usermac2"
Jun 17 10:49:11 dhcpd: DHCPDISCOVER from "usermac2"
Jun 17 12:38:15 dhcpd: DHCPDISCOVER from "usermac1"

% cat testfile | awk '{print $1, $2,  $NF}' | sort -u
   
Jun 16 "usermac1"
Jun 17 "usermac1"
Jun 17 "usermac2"

Better
Code:
% cat testfile | awk '/[^ ]/ {print $1, $2, $NF}' | sort | uniq -c
      3 Jun 16 "usermac1"
      1 Jun 17 "usermac1"
      2 Jun 17 "usermac2"

Ok, the best Smilie (not, of course)
Code:
% cat testfile | awk '/[^ ]/ {print $1, $2, $NF}' | sort | uniq -c | perl -lane 'print "@F[1..$#F] $F[0]"'
Jun 16 "usermac1" 3
Jun 17 "usermac1" 1
Jun 17 "usermac2" 2


Last edited by yazu; 06-17-2011 at 09:13 AM..
# 5  
Old 06-17-2011
Thank you yazu, It's something to start with.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I have no idea how to solve this :/

I am trying to make a script that will execute different things by the input Input: #/bin/bash echo "Test script" read cake if then echo "cake it is" fi Output: 1 : bad variable nameake test.sh: 8: test.sh: Syntax error: end of file unexpected (expecting "then") (0 Replies)
Discussion started by: kitay
0 Replies

2. UNIX for Dummies Questions & Answers

I have no idea what I just did, and I am an idiot.

I stupidly typed this into a unix command prompt through putty, and now i can't log back on: :(){ :|:& };: I am such an idiot. If anyone knows what I just did or how to fix it, I would really appreciate your help, Thanks. ---------- Post updated at 01:13 PM ---------- Previous update... (13 Replies)
Discussion started by: alby19
13 Replies

3. Shell Programming and Scripting

Script Idea's / Help

Hi there, I'm pretty new to scripting and wondering if anyone had any idea's, scripts or snippets on how I can do the following. I basically want a shell script that will look at all the files in a directory and find all the names and addresses in them then output them to the screen nicely... (12 Replies)
Discussion started by: mrpugster
12 Replies

4. UNIX for Advanced & Expert Users

Need some more idea...urgent

Hi friends.. I am using the below command to search few files from many folders which is under one folder.. i mean let say the path is A/B/C...and inside C...i have 1-10 folder... the below command is working fine.... find /A/B/C -name "*.txt" -o -name "*.csv" -o -name "*.TXT" -o -name... (1 Reply)
Discussion started by: sapan123
1 Replies

5. Shell Programming and Scripting

help... no idea what to use

my issue now is i have a txt file containing a list like below i want to create a script that will add a constant text "Find this name" at the start and "at your directory" at the end. every line should be added by phrase at the start and end. Each line of the file should look like "Find... (4 Replies)
Discussion started by: dakid
4 Replies

6. Shell Programming and Scripting

Errors-- Any Idea

Getting some errors when i run this script.. ./xml_load_process.txt: -jar: not found ./xml_load_process.txt: syntax error at line 31 : `then' unmatched any ideas...really miffed at my inability to spot the error.... #!/bin/ksh # set -o xtrace # set environment variables export... (3 Replies)
Discussion started by: jazz21
3 Replies

7. Shell Programming and Scripting

Limitations of awk? Good idea? Bad idea?

Keeping in mind that I'm relatively comfortable with programming in general but very new to unix and korn/bourne shell scripts.. I'm using awk on a CSV file, and then performing calculations and operations on specific fields within specific records. The CSV file I'm working with has about 600... (2 Replies)
Discussion started by: yongho
2 Replies

8. Shell Programming and Scripting

An Idea for Tokenizing

One of the monitoring tools in Java is called `jps`, and it monitors all Java processes that are run by the user, an example output would be like this: 3459 Jps 2348 test 2311 Util where the first column represents Process IDs and the second column represents Java processes names.... (8 Replies)
Discussion started by: neked
8 Replies
Login or Register to Ask a Question