multiple arrays through awk...


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users multiple arrays through awk...
# 1  
Old 07-28-2006
multiple arrays through awk...

i am v new to awk, well unix as a whole really but im enjoying it alot...

im trying to extract some data from a file, and parsing it into arrays, ive trawled for hours on the internet and cant find much when it comes to awk and arrays??

anyway, heres the file:

tableA tableB tableC
1 1 3
2 3 2
3 2 1

tableD tableE tableF
4 4 6
5 6 5
6 3 4

i want to loop through this data, putting the array/array index name as tableA, B, C etc, and then have the numbers as elements of the each array, my long term hope is to pipe these out to a file, where they can perform some kind of sql statement, something like UPDATE tableA where field = '1';

so its essential i can reference the table names at a later stage, these will need piping out too.

its tricky, but ill get the hang of it!

thanks in advance. hopefully i can learn something here and apply it in future.

thanks again.
# 2  
Old 07-28-2006
Here's my stab at it (I sure this can be done better):

Code:
{
print "tableA tableB tableC
1 1 3
2 3 2
3 2 1

tableD tableE tableF
4 4 6
5 6 5
6 3 4
" | nawk '

    # New Column headings 
    /table/ {
        # For each field (column heading)
        for (i=1; i<=NF; i++) {
           # Save the current column headings
           current[i]=$(i)
    
           # Save an array pointer...
           arrs[$(i)]=i
    
           # ...and an array index
           arrs_idx[$(i)]=1
        }
        # Move on
        getline
    }
    
    # Each non column heading record
    {
        # For each column heading (table name)...
        for (i=1; i<=NF; i++) {
            # Create a record in table_array, indexed by the table name and its current index (based on current headings)
            table_array[current[i] OFS arrs_idx[current[i]]++] = $(i)
        }
    }
    
    END {
        # For each table in the table array...
        for (i in arrs) {
            # Print its table name
            print ""
            print i
            print "========="

            # For each record in the array...
            for (j=1; j<=3; j++) {
                #  Print the element
                print table_array[i OFS j]
            }
        }
    }
    '
}

Output:

Code:
tableA
=========
1
2
3

tableB
=========
1
3
2

tableC
=========
3
2
1

tableD
=========
4
5
6

tableE
=========
4
6
3

tableF
=========
6
5
4


Last edited by tmarikle; 07-28-2006 at 08:36 PM..
# 3  
Old 08-17-2006
paste a sample output for your question....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating script with multiple job arrays

Hello everyone, First of all this is my first post and im fairly new to working with Unix and creating scripts etc. so there will probably be wrong phrases used. Lets get to my questions. I have multiple scripts that submit Slurms/Jobs to the cluster starting like this and doing certain... (3 Replies)
Discussion started by: idbemad
3 Replies

2. Programming

Looping through multiple arrays in C.

Not sure if this is possible, but I've tried this about a thousand ways now. I am making something with a lot of arrays. I thought I could put the array names into a separate array and then loop through them to call all of their elements. This is the best I've got so far: #include <stdio.h>... (4 Replies)
Discussion started by: Azrael
4 Replies

3. Shell Programming and Scripting

Curl - upload multiple attachment arrays to server webpage

The html page of the form data is as below <form name="uploadform" id="uploadform" action="htmlupload.php" enctype="multipart/form-data" method="post"> <table class="tborder" cellpadding="6" cellspacing="1" border="0" width="100%" align="center"> <tr> <td class="tcat"> Upload Files ... (0 Replies)
Discussion started by: jaango123
0 Replies

4. Shell Programming and Scripting

Compare multiple arrays elements using awk

I need your help to discover missing elements for each box. In theory each box should have 4 items: ITEM01, ITEM02, ITEM08, and ITEM10. Some boxes either have a missing item (BOX02 ITEM08) or might have da duplicate item (BOX03 ITEM02) and missing another one (BOX03 ITEM01). file01.txt ... (2 Replies)
Discussion started by: alex2005
2 Replies

5. Shell Programming and Scripting

Multiple arrays in variable using for loop

Hi, I'm trying to get the number of files inside same kind of folders on each disks and assigning each values in to a variable named with same folder and disk name so that it'll be easy for me to identify each time.But somehow I'm not able to assign those values in that specific name variable... (1 Reply)
Discussion started by: ratheeshp
1 Replies

6. Shell Programming and Scripting

Loop over multiple arrays

Hi All I need really really help with this :- I have two files ( File1 , File 2) both files are output of two different scripts. File1 usually has a list of names ( sometimes 3 names sometimes 5 sometimes more , depends about the output of the script) File2 usually has a list of numbers... (2 Replies)
Discussion started by: samsan
2 Replies

7. Shell Programming and Scripting

awk arrays comparing multiple columns across two files.

Hi, I'm trying to use awk arrays to compare values across two files based on multiple columns. I've attempted to load file 2 into an array and compare with values in file 1, but success has been absent. If anyone has any suggestions (and I'm not even sure if my script so far is on the right lines)... (4 Replies)
Discussion started by: hubleo
4 Replies

8. Shell Programming and Scripting

awk Arrays

So I'm back once again beating my head off a wall trying to figure out how to get this to work. My end goal is to take input such as what's below, which will be capture in real time with a tail -f from a file or piped output from another command: ... (5 Replies)
Discussion started by: ShadowBlade72
5 Replies

9. Shell Programming and Scripting

Read multiple arrays in mysql

I have a database that include 5 tables, and they are related to each other through foreign key relations. The root is called colleges. There are multiple colleges, and each college has 1+ departments, each department has 1+ IT stuff, each IT stuff owns 1+ IP addresses. I have designed the database... (0 Replies)
Discussion started by: pinkgladiator
0 Replies

10. Shell Programming and Scripting

Nested Loop to Echo Multiple Arrays

I have three arrays which hold three elements each. I have a fourth array which contains the names of those three arrays. I'm having difficulty creating a nested loop that can loop through each array and echo their values. script #!/bin/ksh # array of locations (usa, london, australia)... (1 Reply)
Discussion started by: yongho
1 Replies
Login or Register to Ask a Question