Text > Sort > Array. AWK Program

This is a discussion on Text > Sort > Array. AWK Program within the Linux Programming forums, part of the Platform Specific Boards category; I'm having problem with an bit of school work. I'm meant to be getting a text file, sorting it into ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    21

    Text > Sort > Array. AWK Program

    I'm having problem with an bit of school work.

    I'm meant to be getting a text file, sorting it into descending number order and then loading it into an array. All within a shell script. This is the very first time I've used AWK and although I have read all the literature provided it doesn't seem to cover how to get started.

    So far all I have is:

    Code:
    #!/bin/awk -f
    
    myarray[1]=data.txt
    But obviously that doesn't work.

    Would be great if someone could point me in the right direction, how to define an array from a text file within a shell script using awk.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,050
    Post what the data file looks like, besides the way you're going about it is not the way to read a file using awk.
    Pickup a copy of The AWK Programming Language which is by far the best book from its eponymous authors.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    I think they should've skipped alphabetic order of authors in that book.

    itCbitC +1. That's nice book.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    21
    Thank for the book link, I'll be sure to get a copy.

    The file just looks like:

    2
    13
    546
    32
    23
    5
    2
    123
    34


    Got a little further using the unix sort command piped to awk.

    Code:
    #!/bin/sh
    
    sort -n data.txt |
    
    awk '
    {
      array[$2]=$1
    }
    END{
      for(i in array) {
        print array[i],i
        
      }
    }
    '
    Just can't seem to wrap myself around how to code in awk.
    Last edited by Chems; 11-08-2010 at 04:55 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,050
    Read file into an awk array, then use something like bubble sort to put it in descending order:
    Code:
    awk '{
      a[NR]=$0
    }END{
      <sorting array code here>
      for (i=1;i<=NR;i++)
        print a[i]
    }' data.txt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a text file to an array?
    By Raen in forum C Programming
    Replies: 11
    Last Post: 10-15-2010, 07:26 PM
  2. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 05:03 AM
  3. Writing a C program for class....
    By Bizmark in forum C Programming
    Replies: 10
    Last Post: 11-13-2007, 10:31 AM
  4. Moving to the next structure array
    By mattz in forum C Programming
    Replies: 2
    Last Post: 11-30-2001, 03:43 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21