AWK : Load variables from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK : Load variables from file
# 1  
Old 06-27-2012
AWK : Load variables from file

Hi,

I'm looking for a way for me to load a set of variables (threshold values) from a file using AWK so that when my AWK script processes a file, I can use the threshold values (loaded into an AWK variable) to compare against the ones in the file being processed. If the threshold exceeds or something like that, print it into a file etc... The reason why I want to have have AWK load the threshold values from a file is so that these values can be changed on the fly. I know I can hard code the values but it's not practical.

Here's an example of my config file:
Code:
var1=100
var2=1000
var3=10000
var4=10

I haven't written the script yet because I need to figure out this first... Smilie So, the script should do something like this :
Code:
awk '
BEGIN{
 <load the config file here>
} <do the comparison here, etc>' file

Is this possible at all using AWK? If not, I will have to use shell scripting which would probably be slower consider the file I am going to process is going to be big. Any pointers would be very much appreciated.

Thanks.
# 2  
Old 06-28-2012
if you only have these four variables, then source your file

Code:
 
. variables.txt
 
awk -v var1="$var1" -v var2="$var2" -v var3="$var3" -v var4="$var4" '{do your process and access the vairable as var1, var2, var3, var4}'

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 06-28-2012
Well, I actually have more than 4. Right now, I have 27 of them and the list may just increase later on. The idea is to make it as dynamic as possible so that no changes to the script would be required in the future.
# 4  
Old 06-28-2012
Awk doesn't have an eval so you can't just set arbitrary variable names in BEGIN.

What's your system? If you have GNU awk, you can just add 'BEGIN {' and '}' to your configfile, then do this:

Code:
awk -f configfile.awk -f mainscript.awk ...

It's perfectly valid to have more than one BEGIN { } section, by the way, so this won't interfere with mainscript. The advantage of this is you get full awk syntax in your config file, including quotes and escapes behaving as expected, comment lines with #, and such.

Or you could cram them all onto the commandline programmatically:

Code:
set --

# Rarely-appreciated property of xargs:  it can understand quotes.
# We use it to convert var="asdf" into var=asdf
xargs -n 1 < configfile > /tmp/$$

while read LINE
do
        set -- "$@" -v "$LINE"
done < /tmp/$$

rm -f /tmp/$$

awk "$@" -f script.awk ...


Last edited by Corona688; 06-28-2012 at 03:55 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 06-28-2012
Like this may be:

Code:
awk 'BEGIN{FS="=";while(getline < "configfile") a[$1]=$2} <use variables as a["var1"],a["var2"],etc.>'

This User Gave Thanks to elixir_sinari For This Post:
# 6  
Old 06-28-2012
Thanks Corona688. I didn't think of using the -f option Smilie I'll try this out.

I would also try out the suggestion posted by elixir and see which works out better for me.
# 7  
Old 06-28-2012
Quote:
Originally Posted by Corona688
What's your system? If you have GNU awk, you can just add 'BEGIN {' and '}' to your configfile, then do this:

Code:
awk -f configfile.awk -f mainscript.awk ...

I don't see anything GNU AWK specific there. Perhaps some obsolete implementations do not support multiple -f options, but it's common functionality with modern implementations, e.g., gawk, nawk, mawk, busybox. POSIX has required it for at least 15 years now.

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

2. Shell Programming and Scripting

awk - is it possible to import variables from a config file?

hi, all. I got a question about awk: If I have a lot of awk files for different tasks, but they'll share the same base directory, log setting, action lists, etc., is it possible to import them from a config file, so that I don't have to put them into each awk file? For example, I need a... (8 Replies)
Discussion started by: lan9856
8 Replies

3. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

4. UNIX for Advanced & Expert Users

use of variables in awk to search for pattern from a file

Hi, I need to extract all the content between two strings stored in two variables "startstring" and "endstring" startstring=hello enstring=world #notworking awk '/$startstring/, $NF ~ /$endstring/ ' file > file2 The above code is not working with variables. It works when actual string... (2 Replies)
Discussion started by: jeanjkj
2 Replies

5. Shell Programming and Scripting

How to load an array with desired lines with awk

Hi everyone, Please some help over here. I've written the below script using ranges (/Initial_pattern/,/Final_Pattern/)to extract only those lines of interest for me: awk ' /^Category/,/^$/{print $1}; /Titles/,/^$/{print $1}; /Authors/,/^$/{print $1}' inputfile To process this input:... (8 Replies)
Discussion started by: cgkmal
8 Replies

6. Shell Programming and Scripting

Using AWK BEGIN to extract file header info into variables

Hi Folks, I've searched for this for quite a while, but can't find any solution - hope someone can help. I have various files with standard headers. eg. <HEADER> IP: 1.2.3.4 Username: Joe Time: 12:00:00 Date: 23/05/2010 </HEADER> This is a test and this part can be any size... (6 Replies)
Discussion started by: damoske
6 Replies

7. Shell Programming and Scripting

awk splits file in to multiple - how to get filenames in variables withing ksh?

Hi all, I'm using awk in a .ksh script to split one file by line prefix into different files (up to 4). The files are named after the prefix in the line, and a sequence no. Is there any way to get the filenames in to variables too? I need _ftpfile1, _ftpfile2, _ftpfile3 and _ftpfile4 which are... (2 Replies)
Discussion started by: spidermike
2 Replies

8. Shell Programming and Scripting

awk & CPU Load

Deal All, I'm writing a simple awk to generate some sort of report. The awk will check 24 files (file generated each one hour in a wholoe day) and then it will print one field to another file for counting purposes. The script is working fine but the problem is that the CPU load is very high... (10 Replies)
Discussion started by: charbel
10 Replies

9. Shell Programming and Scripting

Awk/shell question: Read from file and assign to variables.

Is anyone able to help with writing a program that will do the following: 1. Read the contents of a file, line by line, and on each line, assign each of the two columns to a shell variable. 2. perform an action on the variables 3. Read the next line. Here is what I've gotten so far. ... (3 Replies)
Discussion started by: akbar
3 Replies

10. Shell Programming and Scripting

Need help in wrting Load Script for a Load-Resume type of load.

hi all need your help. I am wrting a script that will load data into the table. then on another load will append the data into the existing table. Regards Ankit (1 Reply)
Discussion started by: ankitgupta
1 Replies
Login or Register to Ask a Question