awk - Rotating an array 90 degrees


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - Rotating an array 90 degrees
# 1  
Old 02-02-2009
Question awk - Rotating an array 90 degrees

Hi All,

I have some data (below) that I need to rotate 90 degrees - in other words I want to flip each row into a column.

Quote:
39 45 49 51 56 58 61 64 68 70 72 77
8 8 8 9 9 9 9 10 10 10 10 10
8 8 8 9 9 9 9 10 10 10 10 10
4 4 5 5 6 6 7 7 8 8 8 9
4 4 5 5 6 6 7 7 8 8 8 9
0 0 0 0 0 0 0 0 0 0 0 0
8 8 8 9 10 10 11 12 12 13 14 16
4 5 5 5 6 6 6 7 7 8 8 9
35 38 39 41 43 44 45 47 49 49 50 52
15 16 17 19 19 21 22 24 25 26 28 32
7 8 8 9 9 10 11 12 12 13 13 16
21 22 23 25 26 27 28 29 31 31 32 35
16 19 21 22 24 26 28 30 33 35 36 39
35 37 39 42 44 45 47 51 53 54 56 62
30 33 34 36 38 39 40 42 44 44 45 47
31 38 40 42 44 46 47 49 51 52 53 54
0 0 0 0 0 0 0 0 0 0 0 0
56 65 70 75 82 87 93 99 107 112 117 125
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
I've found the following code in the book "Effective awk programming" but it doesn't work on my input data. I've used arrays before but I can't make it work... can anyone help me out with this one? Smilie

Code:
awk '{
                if (max_nf < NF)
                    max_nf NF
                max_nr = NR
                for (x = 1; x <= NF; x++)
                    vector[x, NR] = $x
} END {
      for (x = 1; x <= max_nf; x++) {
          for (y = max_nr; y >= 1; --y)
              printf("%s ", vector[x, y])
          print("\n")
      }
}' $1

Thanks in advance Smilie

BTW: I've actually removed a text field from $1 (column 1) because I thought it would make it easier to solve - does this make any difference?

Cheers, p.
# 2  
Old 02-02-2009
I've spotted a typo in my translation; correct code should read:

Quote:
awk '{
if (max_nf < NF)
max_nf = NF
max_nr = NR
for (x = 1; x <= NF; x++)
vector[x, NR] = $x
} END {
for (x = 1; x <= max_nf; x++) {
for (y = max_nr; y >= 1; --y)
printf("%s ", vector[x, y])
print("\n")
}
}' $1
This now works but I'd like to find a solution that works on my input file that includes the text column in $1...
# 3  
Old 02-02-2009
Correction again - I must've done something wrong before; the above code works perfectly at converting rows into columns Smilie hurrah!
# 4  
Old 02-02-2009
Congrats man!!!Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with rotating files

Hello: I have a script that gets the ACLs of the /home directory and its contents with getfacl and writes them to a file. The script is run by a cron job and I don't want it to rewrite or append to an already existing file. The point of backing permissions up is because I may need to restore them.... (2 Replies)
Discussion started by: Cacializ
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

Pattern count on rotating logs for the past 1 Hr

Hi All, I have a requirement to write a shell script to search the logs in past 1 hour and extract some pattern from it and count it cumulatively to a file. The problem which I'm facing here is - logs rotates on size basis, say if size of log reaches 5 MB then new log will be generated and... (7 Replies)
Discussion started by: Gem_In_I
7 Replies

4. Red Hat

ip rotating on exim mail server

Hi all, We have the exim mail server configured on cpanel in centos. We have 5 dedicated ip's. So, when i sending mails to client systems, it should be rotate that ip addressees on every 15 minutes. That means Ip rotating. How can i do it. Can anybody show me how to do it. Thanks, (0 Replies)
Discussion started by: mastansaheb
0 Replies

5. Shell Programming and Scripting

Rotating snapshot backup using rsync

I want to take daily backup(11pm) of /var/www to /mnt/bak excluding /var/www/videos and /var/www/old. HOW to implement a rotating snapshot method, so that i can have multiple(say 4) automatically rotating backups. (0 Replies)
Discussion started by: proactiveaditya
0 Replies

6. Solaris

rotating a log yearly

Hi, I am having some troubles using /usr/sbin/logadm to rotate sulog yearly. Can someone please assist with the correct syntax to rotate the sulog yearly? I'd like to maintain up to 3 years of logs. I am on Solaris 10. Thanks, (1 Reply)
Discussion started by: lwif
1 Replies

7. UNIX for Advanced & Expert Users

logrotate isn't rotating files any longer

I have been using logrotate for quite awhile now. Most logs are rotated daily, using /etc/cron.daily. I noticed that the logs in question have not been rotated since April 6, but daily up to that point. I have logrotate in /etc/cron.daily. The basic command is: /usr/sbin/logrotate... (1 Reply)
Discussion started by: manouche
1 Replies

8. Solaris

rotating the syslogd and messages files

Im about to install a sunfreeware program called logrotate which does exactly what it says on the tin....just a quick question ..if its going to rename messages to messages.0 etc do I need to issue a HUP to syslogd after doing this or will the new messages file get created automatically cheers (2 Replies)
Discussion started by: hcclnoodles
2 Replies

9. Shell Programming and Scripting

Rotating a String

Hi folks, I want to rotate a string in Clock or Ani Clock wise. That is If the string is "TAMIL" the out put should be TAMIL AMILT MILTA ILTAM LTAMI TAMIL Please do help. (1 Reply)
Discussion started by: bubeshj
1 Replies
Login or Register to Ask a Question