awk array - first and last occurance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk array - first and last occurance
# 1  
Old 05-20-2006
awk array - first and last occurance

What I'm trying to do is, using awk, loading the log file into an array matching column 2, counting the match and finding the first occurance and last occurance of column 1 being it a date.

08052006:AAA
08052006:AAA
08052006:BBB
09052006:AAA
15052006:BBB
11052006:CCC
19052006:CCC
15052006:BBB
20052006:CCC

This is the output I'm trying to achieve:

ID :Count:First Occurance:Last Occurance
AAA:3 :08052006 :09052006
BBB:3 :08052006 :15052006
CCC:3 :11052006 :20052006

I've managed to produce the first two columns of output (I guess that's the easy part), but I just can't work out how to print the first date occurance and the last

Can anyone give me some pointers, please?
# 2  
Old 05-20-2006
show what you have so far, pls

Also when you say 'first occurance'.... do you mean first occurance in the file OR being numerically first [08052006 occuring before 08062006]?

Last edited by vgersh99; 05-20-2006 at 01:19 PM..
# 3  
Old 05-20-2006
I could post what I've got so far, but the format of the log file is totally different from my example.

What I mean by first and last occurance is the earliest date and the last date an entry was made in the log file for that particular ID.
# 4  
Old 05-20-2006
Okay...did a quick and cut down version of what I have using the example I gave earlier:

/usr/xpg4/bin/awk '{
id_arr[$2]++;
id_dat[$2]=$1;

} END {
for ( x in id_arr ) {
printf "%s:%s:%s:%s\n",x,id_arr[x],id_dat[1],id_dat[NR]
}
}' FS=: test

test file contains:
08052006:AAA
08052006:AAA
08052006:BBB
09052006:AAA
15052006:BBB
11052006:CCC
19052006:CCC
15052006:BBB
20052006:CCC

I thought using id_dat[1],id_dat[NR] would give me the first/earliest date and the last/latest log entry (sorry the log file is sorted in date order)

Bu my output is:
CCC:3::
BBB:3::
AAA:3::
# 5  
Old 05-20-2006
try this one:
Code:
BEGIN {
  FS=OFS=":"
}

{
   id[$2]++
   if ( !($2 in first)) first[$2] = $1
   last[$2]=$1
}
END {
  for(i in id)
    print i, id[i], first[i], last[i]
}

# 6  
Old 05-20-2006
You're a Genius....

Talk about making things look easy.

Thanks very much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

2. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

3. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. Shell Programming and Scripting

AWK help. how to compare a variable with a data array in AWK?

Hi all, i have a data array as follows. array=ertfgj2345 array=456ttygkd . . . array=errdjt3235 so number or elements in the array can varies depending on how big the data input is. now i have a variable, and it is $1 (there are $2, $3 and so on, i am only interested in $1). ... (9 Replies)
Discussion started by: usustarr
9 Replies

5. Shell Programming and Scripting

awk to replace second occurance

#original file . . ~ ~ Index=2 xxx replace #dont replace 1st occurance yyy Index=2 xxx replace #substitue replace with "REPLACE" yyy Index=2 xxx replace #substitue replace with "REPLACE" yyy Index=3 xxx replace (3 Replies)
Discussion started by: cjjoy
3 Replies

6. Shell Programming and Scripting

Count the Consecutive Occurance of "X" in awk

Hi All, I have a data as follow: 0 0 0 X X 0 X X X 0 X 0 0 X 0 0 (16 Replies)
Discussion started by: nica
16 Replies

7. Shell Programming and Scripting

How can I match lines with just one occurance of a string in awk?

Hi, I'm trying to match records using awk which contain only one occurance of my string, I know how to match one or more (+) but matching only one is eluding me without developing some convoluted bit of code. I was hoping there would be some simple pattern matching thing similar to '+' but... (9 Replies)
Discussion started by: jonathanm
9 Replies

8. Shell Programming and Scripting

How to insert values in 1st occurance out of two occurance in a file

Hi I have a file which contains the following two lines which are same But I would like to insert the value=8.8.8.8 in the 1st occurance line and value=9.9.9.9 in the 2nd occurance line. <parameter name="TestIp1" value=""> <parameter name="TestIp1" value=""> Please suggest (1 Reply)
Discussion started by: madhusmita
1 Replies

9. Shell Programming and Scripting

Array in awk

Hi, How can find Array is NULL or not. i wrote script using arrays but i want dislpy an message if arrary was Empty. pls help me on this. (2 Replies)
Discussion started by: koti_rama
2 Replies

10. Shell Programming and Scripting

awk: last occurance

Hi All, I need to extract the last occurance of a pattern match. So far I've got the code below which extracts the first occurance. Any ideas how I can modify it so that it extracts the last? BEGIN {} { if (data++ == 0) ... (17 Replies)
Discussion started by: pondlife
17 Replies
Login or Register to Ask a Question