Deleting keys and values-Awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting keys and values-Awk
# 1  
Old 04-28-2010
Deleting keys and values-Awk

key pair is 1st and 6th column ex:a20 : p10 or a20 : p11
For every key pair if the vlaue(4th column) is the same then delete all the lines who has keypair and the value
ex: a20 : p10 has value 1 only then delete those but a20 : p11 has different values 1,2 and 3 and keep those.

input
Code:
a20    a_a01    a/a    1    p-s    p10    2.5
a20    a_a02    a/a    1    p-s    p10    2.6
a20    a_a03    a/a    1    p-s    p10    2.7
a20    a_a04    a/a    1    p-s    p10    2.8
a20    a_a05    a/a    1    p-s    p11    3.5
a20    a_a06    a/m    2    p-s    p11    3.6
a20    a_a09    h/h    3    p-s    p11    3.7
a20    a_a08    h/h    3    p-s    p11    3.8

output
Code:
a20    a_a05    a/a    1    p-s    p11    3.5
a20    a_a06    a/m    2    p-s    p11    3.6
a20    a_a09    h/h    3    p-s    p11    3.7
a20    a_a08    h/h    3    p-s    p11    3.8

Thanx in advance

Last edited by ruby_sgp; 04-28-2010 at 03:49 AM.. Reason: format problem
# 2  
Old 04-28-2010
# 3  
Old 04-28-2010
its not home work dude!
# 4  
Old 04-28-2010
Sorry but it can't be but homework. You problem description is too foreign from real life questions.
If you want to learn awk, just do it yourself. Don't ask people to do the job for you. This is pointless.
# 5  
Old 04-28-2010
thanx but not thanx

instead of posting answers or suggestions dont' give trash answers to the new users.
If you don't want to answer do not answer.
# 6  
Old 04-28-2010
Quote:
Originally Posted by ruby_sgp
key pair is 1st and 6th column ex:a20 : p10 or a20 : p11
For every key pair if the vlaue(4th column) is the same then delete all the lines who has keypair and the value
ex: a20 : p10 has value 1 only then delete those but a20 : p11 has different values 1,2 and 3 and keep those.

input
Code:
a20    a_a01    a/a    1    p-s    p10    2.5
a20    a_a02    a/a    1    p-s    p10    2.6
a20    a_a03    a/a    1    p-s    p10    2.7
a20    a_a04    a/a    1    p-s    p10    2.8
a20    a_a05    a/a    1    p-s    p11    3.5
a20    a_a06    a/m    2    p-s    p11    3.6
a20    a_a09    h/h    3    p-s    p11    3.7
a20    a_a08    h/h    3    p-s    p11    3.8

output
Code:
a20    a_a05    a/a    1    p-s    p11    3.5
a20    a_a06    a/m    2    p-s    p11    3.6
a20    a_a09    h/h    3    p-s    p11    3.7
a20    a_a08    h/h    3    p-s    p11    3.8

Thanx in advance
I'm not sure if this your desired output based on your explanation - looks like the 2 last lines shouldn't be there.
Code:
nawk '{idx=$1 SUBSEP $6 SUBSEP$4;a[idx]++;b[idx]=$0}END {for(i in a) if (a[i]==1) print b[i]}' myFile

# 7  
Old 04-28-2010
Something like that ?
Code:
sort -k1,1 -k6,6 ruby.dat |
awk '
function proceed_key(    ) {
   if ((keep_key || count==1) && memo) print memo;
   count    = 0;
   memo     = "";
   keep_key = 0;
   prv_val  = val;
}
{
   key = $1 SUBSEP $6;
   val = $4;

   if (key != prv_key) proceed_key();
   prv_key = key;
   count++;

   memo = (memo ? memo "\n" : "") $0;
   if (val != prv_val) keep_key = 1;
}
END {
   proceed_key();
}
  '

Input file:
Code:
a19    a_a00    a/a    0    p-s    p00    2.0
a20    a_a01    a/a    1    p-s    p10    2.5
a20    a_a02    a/a    1    p-s    p10    2.6
a20    a_a03    a/a    1    p-s    p10    2.7
a20    a_a04    a/a    1    p-s    p10    2.8
a20    a_a05    a/a    1    p-s    p11    3.5
a20    a_a06    a/m    2    p-s    p11    3.6
a20    a_a09    h/h    3    p-s    p11    3.7
a20    a_a08    h/h    3    p-s    p11    3.8
a20    a_a08    h/h    1    p-s    p12    3.9
a20    a_a18    h/h    1    p-s    p12    3.9
a21    a_a18    h/h    1    p-s    p12    3.9
a21    a_a18    h/h    2    p-s    p12    3.9
a22    a_a18    h/h    0    p-s    p13    3.9

Output:
Code:
a19    a_a00    a/a    0    p-s    p00    2.0
a20    a_a05    a/a    1    p-s    p11    3.5
a20    a_a06    a/m    2    p-s    p11    3.6
a20    a_a08    h/h    3    p-s    p11    3.8
a20    a_a09    h/h    3    p-s    p11    3.7
a21    a_a18    h/h    1    p-s    p12    3.9
a21    a_a18    h/h    2    p-s    p12    3.9
a22    a_a18    h/h    0    p-s    p13    3.9

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Calculate average from a given set of keys and values

Hello, I am writing a script which expects as its input a hash with student names as the keys and marks as the values. The script then returns array of average marks for student scored 60-70, 70-80, and over 90. Output expected 50-70 1 70-90 3 over 90 0 The test script so far... (4 Replies)
Discussion started by: nans
4 Replies

2. Shell Programming and Scripting

Deleting consecutive equal values in a file

Hello everyone, I have a requirement as shown below. I need to delete consecutive same values from the source file and give it as output file. Source: a,b,c,d,e,e,f,g Target: a,b,c,d,f,g The repeating value "e" should be deleted from the file completely. How can I achieve this... (14 Replies)
Discussion started by: vamsikrishna928
14 Replies

3. Shell Programming and Scripting

Deleting values in a column based on conditions

Hi I have a difficulty in writing shell script for performing the tasks. A B C D 12 230 16 259 18 260 23 283 21 291 36 298 41 309 49 420 52 425 57 450 61 456 70 473 72 475 79 486 If the A(row no.2) < C(row no.1) then delete value A(row no.1) and so on... For... (8 Replies)
Discussion started by: Sarwagya Jha
8 Replies

4. Shell Programming and Scripting

Extract values of duplicate keys

I have two questions that are related, so it would be great if you can help me with both! Question1: I have a file A that looks like this: a x b y b z c w I want to get something like: a x b y; z c w Given that a,b,c has no spaces. But the other letters might contain spaces. ... (2 Replies)
Discussion started by: Viernes
2 Replies

5. Shell Programming and Scripting

Deleting row if all column values are a particular string

Hello, I have a very large file for which I would like to remove all rows for which the value of columns 2-5 is zero. For instance I would like this file: contig1, 0, 0, 0, 0 contig2, 1, 3, 5, 0 contig3, 0, 0, 0, 0 contig4, 0, 5, 6, 7 To become this file: contig2, 1, 3, 5,0 ... (17 Replies)
Discussion started by: mouchkam
17 Replies

6. UNIX for Dummies Questions & Answers

Deleting octal values

I have some junk values in my files 鵶„‰¼±¤¡ad. am able to find the octal values as below by using od command. 303 251 265 266 204 211 274 261 244 241 141 144 i want to know how to delete the octal this values . (5 Replies)
Discussion started by: vino.paal
5 Replies

7. Shell Programming and Scripting

comparing the values of repeated keys in multiple columns

Hi Guyz The 1st column of the input file has repeated keys like x,y and z. The ist task is if the 1st column has unique key (say x) and then need to consider 4th column, if it is + symbol then subtract 2nd column value with 3rd column value (we will get 2(10-8)) or if it is - symbol subtract 3rd... (3 Replies)
Discussion started by: repinementer
3 Replies

8. Shell Programming and Scripting

select values based on keys

HI The input 1st column has specific keys like 1 with value a,b and c. 2 with b,b,d and 3 with a,a a. when ever c appears as one of the values the result will be key ........ c (You can see in the out put as 1 w...... 6.... c) and same follows for d. Thanx:) I'm learning awk scripting. If... (3 Replies)
Discussion started by: repinementer
3 Replies

9. Shell Programming and Scripting

Deleting values with specific characters

I have a file with 3 columns 2 4 5 2 4 7 3 5 7 4 -6 9 5 -9 4 6 -3 3 Bascially I want to delete the entire row if column 2 is a "-" So the end result will be 2 4 5 2 4 7 3 5 7 I have trouble doing this cause of the - in front of the number. thanks (2 Replies)
Discussion started by: kylle345
2 Replies

10. Shell Programming and Scripting

How to print Dissimilar keys and their values?

Hi guyz I have been using this script to find similar keys in 2 files and merge the keys along with their values. Therefore it prints similar keys by leaving dissimilar. Any one knows how to print only Dissimilar leaving Similar. Help would be appreciated. The script I'm using for similar... (4 Replies)
Discussion started by: repinementer
4 Replies
Login or Register to Ask a Question