Awk is killing me.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk is killing me.
# 1  
Old 10-09-2011
Awk is killing me.

Hello there,

I have been trying to get my awk script done and I am hanging in the last part for long hours. Just wish if someone could help me.

The actual op is:-
Code:
22    0    0    0
0    0    0    0
0    0    18    0
0    0    0    30
0    0    0    0
0    0    0    0
0    0    0    0
0    0    0    0
0    0    0    0
0    0    0    0
0    0    0    0
0    0    0    0

Desired OP
Code:
21    14    17    29
22    0    18    30
23    0    19    31
24    0    0    32
0    0    0    0
0    0    0    0
0    0    0    0
0    0    0    0
0    0    0    0
0    0    0    0
0    0    0    0
0    0    0    0

Input file array for this output is:-
#### Flavor,DonutValue #####
Code:
0,21
0,22
3,29
0,23
2,17
1,41
0,24
3,30
3,31
3,32
2,18
2,19

### A DOZEN COLLECTED ###

### LOOP COMPLETED ###


CODE SNIPPET.


Code:
!/bin/bash
for variable in c1   #c1 is the filename
do
        no_of_dozen=0
        awk 'BEGIN {RS="\n";FS=","}{
        print "Line after awk =",$0
       
        
        if ( $0 == "#### Flavor,DonutValue #####" ) {
                        no_of_dozen=$no_of_dozen+1
                        for (a=0;a<12;a++)
                                for (b=0;b<4;b++)
                                        ar[b,a]=0
                        a1=0
                        a2=0
                        a3=0
                        a4=0
        }
        else if ( $0 == "### A DOZEN COLLECTED ###" ) {
                        for (a=0;a<12;a++)
                                print ar[0,a] "\t" ar[1,a] "\t" ar[2,a] "\t" ar[3,a]
        }
        else if ( $0  == "### LOOP COMPLETED ###" ) {
                        LOOP+=1
                        no_of_dozen=0
        }
        else if ( $0 == "" )
                        print "DO NOTHING"
        else   {
  
                if ( $1 == 0 ) {  
                        ar[0,$a1]=$2  
                        print "ar_a1=" ar[$1,$a1]
                        print "a1=" a1
                        a1+=1
                }
                else if ( $1 == 1 ) {
                        ar[1,$a2]=$2   
                        print "ar_a2=" ar[$1,$a2]
                        print "a2=" a2
                        a2+=1
                }
                else if ( $1 == 2 ) {
                        ar[2,$a3]=$2      
                        print "ar_a3=" ar[$1,$a3]
                        print "a3=" a3
                        a3+=1
                }
                else if ( $1 == 3 ) {
                        ar[3,$a4]=$2     
                        print "ar_a4=" ar[$1,$a4]
                        print "a4=" a4
                        a4+=1
                }
        }
        }' $variable
done

Thank You all. Any guidance of why I am not able to do get this right will be helpful. I know this is not the best awk code as I have just started.

Cheers. Smilie

P.S. :The idea is to display in array format for columns with header of "0","1","2","3" and the corresponding numbers below it.

Last edited by radoulov; 10-09-2011 at 04:05 AM.. Reason: Code tags!
# 2  
Old 10-09-2011
A sketch. If you don't have too many flavors:
Code:
cat INPUTFILE
0,21
0,22
3,29
0,23
2,17
1,41
0,24
3,30
3,31
3,32
2,18
2,19

awk -F, '{ print $2 > $1 }' INPUTFILE

# GNU sed  Change to literal tabs and -e ':a' -e '...' for an old one.
paste [0-9]* | sed  ':a s!\t\t!\t0\t!; ta'  
21	41	17	29
22	0	18	30
23	0	19	31
24	0	0	32

===

I don't see any easy way to do it with awk only. It would be easy but awkward with Perl. Python or Ruby or even Javascript are better.

Last edited by yazu; 10-09-2011 at 01:50 AM..
# 3  
Old 10-09-2011
Try this
Code:
#!/bin/bash
awk -F, ' {a[$1]=a[$1]" "$2}  
END{split(a[0],_0," ");split(a[1],_1," ");split(a[2],_2," ");split(a[3],_3," ")
i=1; while(i<=NR){printf _0[i]+0"\t"_1[i]+0"\t"_2[i]+0"\t"_3[i++]+0"\n"}}'   input_file

--ahamed

Last edited by ahamed101; 10-09-2011 at 04:27 AM..
# 4  
Old 10-09-2011
Another one:

Code:
awk -F, 'END {
  for (i = 0; ++i <= NR;)
    for (j = mn; j <= mx; j++)
     printf "%d%s", d[++tt[j], j], \
       (j < mx ? OFS : RS)
  }
{
  d[++t[$1], $1] = $2
  $1 > mx && mx = $1
  (mn > $1 || $1 == 0) && mn = $1
  }' OFS='\t' infile

In some awk implementations NR is not available in the END block,
if you're using one of those, you'll need to save its value while reading the input file.
# 5  
Old 10-09-2011
Quote:
Originally Posted by radoulov
In some awk implementations NR is not available in the END block,
if you're using one of those, you'll need to save its value while reading the input file.

Advice taken... Smilie

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Killing the process ID's

Hi , I have a list of application process id's. Is there a way to kill all the process listed below using the script, except the once which are starting with " Genesis " adm 1522 ABC_Process.tra adm 1939 Genesis_Process.tra adm 2729 Genesis_Archive.tra adm 3259 xyz_Process.tra (5 Replies)
Discussion started by: murali1687
5 Replies

2. Shell Programming and Scripting

Killing a subshell

I am calling a script from with another script and reading its output one line at a time (using <childscript> | while read line) in the parent script. If the output exceeds a predefined number of lines I want to kill the child shell from within the parent shell. I decided to print the process ID... (2 Replies)
Discussion started by: slash_blog
2 Replies

3. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

4. Shell Programming and Scripting

Killing process!!!!

Hi friends, i m in big trouble.... i have one script which connects two server ...like below.. script1.sh ------------------------------------- bash test.sh & eval x=$@ export x=`echo $x` #echo $x # ssh user@8.2.5.6 bash /mbbv/location/script.sh $x|sed '/Binary file/d'... (1 Reply)
Discussion started by: Shahul
1 Replies

5. Shell Programming and Scripting

Help in killing the session !

Hello, I am new to unix scripts and have been experimenting with do-while structure. I have run into a problem. my script is, --- while true do ./redir --lport=55083 --caddr=14.121.119.21 --cport=55083 2>&1 >> /tmp/out.txt echo "Restarting `date`" done --- As you can see, i didnt handle... (3 Replies)
Discussion started by: script_newbie
3 Replies

6. UNIX for Dummies Questions & Answers

killing the process

Hi, First, I am running a scipt.While the script is running I realize that I dont want the script to be run so I am killing the script externally.Before the process gets terminated or killed it should delete all the temporary files created by the script.How to do this?Can anyone help me? ... (3 Replies)
Discussion started by: arthi
3 Replies

7. What is on Your Mind?

DNS killing me

Does anyone here know DNS backwords and forwards to include knowing how to set it up for internal and external users (private and public ip addresses)??? Any help would be great! (1 Reply)
Discussion started by: nbredthauer
1 Replies

8. SCO

users killing

:confused: When users logout from the server 'who -u' command shows existance of the user without any prcess ID and 'kill -9' command becomes invalid. Any body have any idea how to solve this problem. let me know please. (0 Replies)
Discussion started by: utpol_68
0 Replies

9. UNIX for Dummies Questions & Answers

Killing Connections

Say, for instance, that you are running a website. You are playing around, using netstat, etc. You notice all the people whom are connected to the site. You then wonder if there is a way to kill one or more of these connections. However, they are not PIDs so could you use the kill command? I was... (1 Reply)
Discussion started by: Phobos
1 Replies
Login or Register to Ask a Question