Compare value in the last occurrence for each id


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare value in the last occurrence for each id
# 1  
Old 03-10-2014
Compare value in the last occurrence for each id

Dear Gents,

Please can you help me.

I have a file with multiple values called ID

ID ( columns 11-24)
INDEX ( column 26 )
STATUS ( columns 91-92)
The ID can be repetead many times to diference each one there is a value called INDEX which difference each time, it increase if the ID is repetead..
I need please to check if the last ocurrence for each ID contens values == to 14 or 98 if this is the case I will like to display the results as the file attached.

Thanks for your support Smilie

Last edited by vbe; 03-10-2014 at 03:03 PM.. Reason: typo..
# 2  
Old 03-10-2014
Isn't this same as this one [Solved] Extract information according to values ​​defined | Unix Linux Forums | Shell Programming and Scripting?
If not, how different is this?

---------- Post updated at 11:41 AM ---------- Previous update was at 11:35 AM ----------

And honestly, i didn't quite understand your requirement.
Also, the id you are talking about spans across 2 columns and stops just before the decimal point?
# 3  
Old 03-10-2014
Hello anamed101.

The input file is not the same that the one in [Extract information]..
Please help me,, only what I need is to verify if the last 2 ocurrences for each ID does not contens values like 14 or 98 in the columns 91-92. And if is the case display.. how many times these values apper.

Example for ID
Code:
2483521305 index 2 has 2 times status 14

I would like to get a list like that becuase I will use it to remove these values from a big list.

ID ( columns 11-24)
INDEX ( columna 26)
STATUS ( cloumnas 91-92)

I stop before the decimal point because for all the IDs the .0 is the same... only the diference is the value INDEX which increase 1 every time when the ID repeat.. Normally each ID has 2 times the same value
Really I appreciate your help
# 4  
Old 03-10-2014
Try this
Code:
awk '{
       status=substr($0,91,2)
       ind=substr($0,26,1);
       split(substr($0, 11, 24-11), val,/\.0 /);
       key[NR%2":"val[1]val[2]] = ind":"status
     }
     END{
       for(i in key){
         split(key[i], brr, /:/)
         if(!(brr[2] == 14 || brr[2] == 98)){
           delete key[i]
           continue
         }
         cnt[i]++
      }
      for(i in key){
        split(i, arr, /:/)
        split(key[i], brr, /:/)
        print arr[2]" index "brr[1]" has "cnt[i]" times status "brr[2]
      }
    }' infile | sort -k1,1n -u

This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 03-11-2014
Dear ahamed101

Thansk a lot for your help,, the scrip works fine. but theres ias a problem in
has "cnt[i]" It always give a value 1?
for example in the ID 2470121241 shouldbe 2 times status 14 not only 1 time?
Please can you help me.
Thanks again
# 6  
Old 03-11-2014
Try this...
Code:
awk '{
       status=substr($0,91,2)
       ind=substr($0,26,1);
       split(substr($0, 11, 24-11), val,/\.0 /);
       key[NR%2":"val[1]val[2]] = ind":"status
     }
     END{
       for(i in key){
         split(i, arr, /:/)
         split(key[i], brr, /:/)
         if(!(brr[2] == 14 || brr[2] == 98)){
           delete key[i]
           continue
         }
         cnt[arr[2]]++
      }
      for(i in key){
        split(i, arr, /:/)
        split(key[i], brr, /:/)
        print arr[2]" index "brr[1]" has "cnt[arr[2]]" times status "brr[2]
      }
    }' infile | sort -k1,1n -u

This User Gave Thanks to ahamed101 For This Post:
# 7  
Old 03-11-2014
Dear ahamed101
I try to use
Code:
cnt[arr[2]]++

but it give empty value,
I try with
Code:
cnt[brr[2]]++

But it still not working.. it accumulate all values and not for each ID
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep util last occurrence

I have file contents /tmp/x/abc.txt /home/bin/backup/sys/a.log I need this output: /tmp/x/ /home/bin/backup/sys/ Can somebody please help me out Please use CODE tags as required by forum rules! (3 Replies)
Discussion started by: jhonnyrip
3 Replies

2. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

3. Shell Programming and Scripting

Substitute first occurrence of keyword if occurrence between two other keywords

Assume a string that contains one or multiple occurrences of three different keywords (abbreviated as "kw"). I would like to replace kw2 with some other string, say "qux". Specifically, I would like to replace that occurrence of kw2 that is the first one that is preceded by kw1 somewhere in the... (4 Replies)
Discussion started by: M Gruenstaeudl
4 Replies

4. UNIX for Dummies Questions & Answers

Sed, last occurrence

How to find last occurrence of a keyword in a file using sed. (4 Replies)
Discussion started by: nexional
4 Replies

5. Shell Programming and Scripting

number of occurrence

: i need a bash script to convert the displayed output 12 14 15 12 15 13 to 12 * 2 ,13 * 1,14*1,15*1 Thanks, nevil (2 Replies)
Discussion started by: nevil
2 Replies

6. UNIX for Dummies Questions & Answers

Breaking up at the second occurrence

hi, My input is: 123 1234|123|123|123 123|123|456 123|123|12 12 Expected output is: 123 1234|123 123|123 123|123 456 123|123 12 (1 Reply)
Discussion started by: pandeesh
1 Replies

7. Shell Programming and Scripting

Last occurrence of code between two tags

Hi, I am new to linux and have a challenge while I am debugging my application logs n linux boxes. our log file xxx.log will have different responses coming in its way while an user logs in. Each response might be of 2000 lines or more. Many users do login at a time and our log file goes big... (24 Replies)
Discussion started by: nariwithu
24 Replies

8. Shell Programming and Scripting

count a occurrence

I am looking to get a output of "2 apple found" from the awk command below. black:34104 tomonorisoejima$ cat tomo apple apple black:34104 tomonorisoejima$ awk '/apple/ {count++}END{print count " apple found"}' tomo 1 apple found black:34104 tomonorisoejima$ (5 Replies)
Discussion started by: soemac
5 Replies

9. Shell Programming and Scripting

Replace second occurrence only

HPUX /bin/sh (posix) I have a file as such cat dog mouse deer elk rabbit mouse rat pig I would like to replace the second occurrence of mouse in this file with mouse2. The rest of the file has to stay exactly as is. I'm not sure exactly where mouse might be (could be first,second,third... (5 Replies)
Discussion started by: lyoncc
5 Replies

10. UNIX for Dummies Questions & Answers

awk + last occurrence

Hi, I'm attempting to search, using awk, a pattern range in a file. Something like: >awk '/first bit of text.../,/...last bit of text/' file Is it possible to print only the last (or first) occurrence of the pattern range this way? Thanks for any suggestions. Al (2 Replies)
Discussion started by: agibbs
2 Replies
Login or Register to Ask a Question