Results 1 to 5 of 5

Thread: awk help wanted

  1. #1
    Join Date
    Mar 2004
    Posts
    6

    Unanswered: awk help wanted

    Hi, I am new to bash programming.

    I have different tab-separated text files with several lines each having colums that look like that :

    ID Designation Price Stock
    065 Soap 2.00 30
    780 Parachute 56.00 2

    Each file can have different field names and different number of fields.

    I would like to retieve the field names and create variables ,one for each field ,with a name corresponding to a field name and whose value would be the field number.(e.g ID=1,Stock=4...)
    I would then like to use these variables in the rest of my script
    I came close using an awk but couldn't export anything back to the script

    Any help would be greatly appreciated

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    You can do something like this :

    Code:
    #
    # Function : FileFields input_file ...
    # Output   : Fields assignments for each field of each record
    #
    FileFields() {
       awk '
            # First record of input_file contains field names
            # which are memorized in the col[] array.
            FNR==1 {
               col_count=NF;
               for (f=1; f<=NF; f++)
                  col[f] = $f;
               next;
            }
            # Data record
            # Assignment for each file is generated in the form :
            # field_name='field_value';
            {
               for (f=1; f<=col_count; f++)
                 printf("%s=\047%s\047; ", col[f], $f);
                 print "";
            }
           ' $*
    }
    
    #
    # Proceed input files
    # The input files are read with the FileFields function
    #
    FileFields cols.txt | \
    while read fields
    do
       eval $fields
    
       # Proceed record
       # Displays assignments of variables as an example
       echo $fields
    
    done

    The result of the execution of this script with your input datas is :

    ID='065'; Designation='Soap'; Price='2.00'; Stock='30';
    ID='780'; Designation='Parachute'; Price='56.00'; Stock='2';
    Jean-Pierre.

  3. #3
    Join Date
    Mar 2004
    Posts
    6
    Thanks for your help, but that's not exactly what i meant

    what i would like is to declare variables in my bash script that would have the name of a field and the position in the inventory file as value

    for example ,with this inventory file(tab-separated):
    ID Designation Price Stock
    065 Italian Wine 2.00 30
    780 Parachute 56.00 2

    i would like to be able to use in the rest of my script (and outside the awk) the variables ID,DESIGNATION,PRICE,STOCK which values are:
    ID=1
    DESIGNATION=2
    PRICE=3
    STOCK=4
    instead of having to declare them

    for this other inventory file:
    Name Surname Age
    Keeler John 41
    King Peter 32

    i would like to be able to use in the rest of my script the variables
    NAME=1
    SURNAME=2
    AGE=3

    Sorry for the the misunderstanding.Your help would be greatly appreciated because i can't find the solution and it's driving me crazy!

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Try this new script :

    Code:
    #
    # Function : GetFields - Get field names and positions
    # Args(s)  : $1 = Input file name (Record 1 = Field names)
    #            $2 = Prefix for variable names (Def="")
    # Env.     : A variable is created for each field of the file
    #            in the form <Prefix><FIELDNAME> and is assigned
    #            the field position.
    #
    GetFields() {
       local fields=$( awk '
            NR==1 {
               for (field=1; field<=NF; field++)
                  printf("%s%s=%d;", PREFIX, toupper($field), field);
               print "";
               exit;
            }
           ' PREFIX="$2" $1 )
       eval $fields
    }
    
    #
    # M A I N . . .
    #
    GetFields input_field FIELD_
    set | grep '^FIELD_'
    The result for your inventory file is :

    FIELD_DESIGNATION=2
    FIELD_ID=1
    FIELD_PRICE=3
    FIELD_STOCK=4

    In my example, th variable are prefixed by FIELD_
    This prefix is optional.
    Jean-Pierre.

  5. #5
    Join Date
    Mar 2004
    Posts
    6

    Smile

    Thanks!!!
    I don't think i can be of any assistance to you in bash scripting
    but i really owe you one

    If i encounter another problem that's definitely here that i'll post it

    thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •