If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > awk help wanted

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-13-04, 09:14
db_Romain db_Romain is offline
Registered User
 
Join Date: Mar 2004
Posts: 6
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
Reply With Quote
  #2 (permalink)  
Old 03-13-04, 10:52
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #3 (permalink)  
Old 03-13-04, 11:30
db_Romain db_Romain is offline
Registered User
 
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!
Reply With Quote
  #4 (permalink)  
Old 03-13-04, 11:57
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #5 (permalink)  
Old 03-13-04, 12:23
db_Romain db_Romain is offline
Registered User
 
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!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On