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
# Function : FileFields input_file ...
# Output : Fields assignments for each field of each record
# First record of input_file contains field names
# which are memorized in the col array.
for (f=1; f<=NF; f++)
col[f] = $f;
# Data record
# Assignment for each file is generated in the form :
for (f=1; f<=col_count; f++)
printf("%s=\047%s\047; ", col[f], $f);
# Proceed input files
# The input files are read with the FileFields function
FileFields cols.txt | \
while read fields
# Proceed record
# Displays assignments of variables as an example
The result of the execution of this script with your input datas is :
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:
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
Sorry for the the misunderstanding.Your help would be greatly appreciated because i can't find the solution and it's driving me crazy!
# 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.
local fields=$( awk '
for (field=1; field<=NF; field++)
printf("%s%s=%d;", PREFIX, toupper($field), field);
' PREFIX="$2" $1 )
# M A I N . . .
GetFields input_field FIELD_
set | grep '^FIELD_'