awk script problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script problem
# 1  
Old 03-02-2010
Question awk script problem

Hi All,

I have the following input data:

Quote:
2008-02-02 3 151
2008-02-02 4 17004
2008-02-03 1 36
2008-02-05 1 95
2008-02-05 3 168
2008-02-05 4 7
2008-02-06 1 383
2008-02-06 2 88
2008-02-07 1 86
2008-02-09 4 33
That I'd like to look like this ($2 is the column I'd like it to appear in) where the entries are grouped by date:

Quote:
0,0,151,17004,2008-02-02
36,0,0,0,2008-02-03
95,0,168,7,2008-02-05
383,88,0,0,2008-02-06
86,0,0,0,2008-02-07
0,0,0,33,2008-02-09
The code I have at present is:

Code:
awk 'BEGIN {}
{
        dt = $1
        if (dt == dt_prev) {
                pp[$2] = $3
        } else {
                if (dt_prev) {
                        print pp[1]","pp[2]","pp[3]","pp[4]","dt_prev
                }
        dt_prev = dt
        for (i in pp) pp[i] = 0
        }
}
END {
        print pp[1]","pp[2]","pp[3]","pp[4]","dt
}' $1

I think I'm pretty close but there's a flaw in the above code - any help would be much appreciated Smilie

Thanks Smilie
# 2  
Old 03-02-2010
Another solution.

Code:
awk ' {  B[$2]=$3;
 for(i=0;i<=4;i++) { if (!B[i]) B[i]=0; }    
 A[$1]=B[1]","B[2]","B[3]","B[4]; } 
END { for (i in A) { print A[i]","i; } }' file

# 3  
Old 03-02-2010
Thanks Dennis,

Unfortunately my input comes out like this when I try your code:

Quote:
2381,4414,870,509,2009-10-02
3755,3809,78560,78257,2009-08-12
916,1144,67,74198,2009-08-13
1492,16,11165,1262,2008-03-11
2303,1,763,1279,2008-12-10
1492,1274,11165,1262,2008-03-13
10,100351,40527,48814,2009-01-21
1097,1036,2187,1279,2008-12-12

Scratch that! It works Smilie Thanks!!

I just misread the output - it's not sorted and that confused me (not difficult Smilie )

Brill, thanks again Smilie

Last edited by pondlife; 03-02-2010 at 12:00 PM.. Reason: I made a mistake!
# 4  
Old 03-02-2010

Hi Pondlife,

Did you test the script throughly , i think the group by is missing in the script suggested by Dennis(you can observe the duplicate values in the fourthe column).

Output from the script(input taken from your first script)
Code:
 
0,0,151,17004,2008-02-02
36,0,151,17004,2008-02-03
95,0,168,7,2008-02-05
383,88,168,7,2008-02-06
86,88,168,7,2008-02-07
86,88,168,33,2008-02-09

You can try something like this:

Code:
awk ' {  if(pr==$1){B[$2]=$3;for(i=0;i<=4;i++) { if(!B[i]) B[i]=0; }} else {for(i=0;i<=4;i++){B[i]=0}; B[$2]=$3};pr=$1; A[$1]=B[1]","B[2]","B[3]","B[4]; }
        END { for (i in A) { print A[i]","i; } }' input_file


Last edited by panyam; 03-02-2010 at 12:51 PM..
# 5  
Old 03-02-2010
Quote:
Originally Posted by panyam
Hi Pondlife,

Did you test the script throughly , i think the group by is missing the script suggested by Dennis(you can observe the duplicate values in the fourthe column).

Output from the script(input taken from your first script)
Code:
 
0,0,151,17004,2008-02-02
36,0,151,17004,2008-02-03
95,0,168,7,2008-02-05
383,88,168,7,2008-02-06
86,88,168,7,2008-02-07
86,88,168,33,2008-02-09

Corrected it.

Code:
awk ' {  if(!A[$1]) { B[1]=B[2]=B[3]=B[4]=0; }
           B[$2]=$3;
           for(i=0;i<=4;i++) { if (!B[i]) B[i]=0; }
          A[$1]=B[1]","B[2]","B[3]","B[4]; }
         END { for (i in A) { print A[i]","i; } }' file

output:

Code:
0,0,151,17004,2008-02-02
36,0,0,0,2008-02-03
95,0,168,7,2008-02-05
383,88,0,0,2008-02-06
86,0,0,0,2008-02-07
0,0,0,33,2008-02-09


Last edited by dennis.jacob; 03-02-2010 at 12:53 PM.. Reason: Updated with output
# 6  
Old 03-02-2010
This should work
Code:
# awk 'END{for(x in a){z=w;for(y=0;++y<5;){z=z ((b[x y])?b[x y]:0)","}print z x}}{a[$1];b[$1 $2]=$NF}' infile
0,0,151,17004,2008-02-02
36,0,0,0,2008-02-03
95,0,168,7,2008-02-05
383,88,0,0,2008-02-06
86,0,0,0,2008-02-07
0,0,0,33,2008-02-09


Last edited by danmero; 03-02-2010 at 01:15 PM.. Reason: Shorter
# 7  
Old 03-02-2010
You guys are awsome Smilie

Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script problem

Hello guys i have following problem. I'm trying to copy content of one file and paste this content in all .txt files in directory, but at line 15. My script copy the content at first line, not 15. I'm confused how to do this. Thank you in advance for your help! This is my script: ARGS=2 ... (9 Replies)
Discussion started by: r00ty
9 Replies

2. Shell Programming and Scripting

Awk Script Problem

Can someone please explain to me what is wrong with this awk script? echo 74 85 | awk '{ if ( $1 > $2 ) PRESULTS = ( $1 - $2 ); print $0,"=>","P"PRESULTS ; else if ( $1 > $2 ) NRESULTS = ( $2 - $1... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

problem with awk script

Hi, I have two files Hi, I have two files file1 :> Code: val="10" port="localhost:8080"httpadd="http:\\192.168.0.239" file2 :> Code: val=${val} val="pdssx" port=${port}port="1324"httpadd=${httpadd}httpadd="raamraav"fileloc=${fileloc} file3(or file2) should have following... (1 Reply)
Discussion started by: nitin.pathak
1 Replies

4. Shell Programming and Scripting

Awk script Problem

Hi , I am having two files FILE1 and FILE2 as shown below I need to search each and every element of Coulumn1 in the FILE1 in FILE2 and Globally replace with the Corresponding element of the Column2 in the FILE2 , For example and1 which is the first element of COl 1 of the FILE1 should be... (4 Replies)
Discussion started by: jaita
4 Replies

5. Shell Programming and Scripting

Problem with awk script

Hi, I have one csv file with 3 fileds like tmp1.csv 2079|2010Aug|cardilogy 2349|2010Aug|numerology 2213|2010Aug|immunlogy another csv file with code for those specialities spec.csv cardiology|CRD numerology|NMY immunology|IMY i want to replace the contents of file 1 with codes... (2 Replies)
Discussion started by: Man83Nagesh
2 Replies

6. Shell Programming and Scripting

Problem with an awk Script

hello, first, yes i searched the forum , google and read many tutorials but still have a problem with my script. I have great Problems, because i haven't worked with regular expressions before and never had anything to do with shellscripts. i am a complete Newby in this sort of theme. I have... (8 Replies)
Discussion started by: Crashvogel
8 Replies

7. Shell Programming and Scripting

Problem with a AWK Script

Hi I am having some contents in my file like this file1 ########################## pin (PIN1) { direction : input ; capacitance : 121 ; max_transition : 231 ; } pin (PIN2) { direction : input ; capacitance : 124 ; max_transition : 421 ;... (8 Replies)
Discussion started by: kshitij
8 Replies

8. Shell Programming and Scripting

Problem with awk script

Hi Can anyone help me in this Problem File1 ######################### HOLI 123 AND ONE TWO THREE AMITABH SAMSUNG POLI AND TWO SENSE CRYING WING PPIN TBFLAG I B AND OROLE TB_HOT=" DCT" TB_CAT=" CAT" TC_NOT=" AND" +PIN TB=" HOT" TB_GATE=" KOT" TB_LATE=" MAT" TC=LOT MAT DAT SAT... (5 Replies)
Discussion started by: kshitij
5 Replies

9. Shell Programming and Scripting

Problem with one awk script

Hi , I am having a file having the contents like this file1 ##################### kite kshitij jolly admire in the wing and tell me the secret behind opus 123 and the right of the track ######################### I have to write one awk script to substitue some values with other... (6 Replies)
Discussion started by: kshitij
6 Replies

10. Shell Programming and Scripting

awk script Problem

I wrote a awk but doesnt work as expected. The Input File attached input file My awk Script /^.......*EXEC CICS /,/END-EXEC/ { if ( $0 ~ / LINK / ) { tsflag=1 } if ( $0 ~ /EXEC CICS/ && tsflag == 1 ) ... (6 Replies)
Discussion started by: pbsrinivas
6 Replies
Login or Register to Ask a Question