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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - is it possible to import variables from a config file?
# 1  
Old 03-05-2013
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 variable LogFileHeader_StartServer="StartServerLog_".
For now, I have to write it into all my awk files.
And if someday I'll change it to another string, I'll have to modify all the files.

But If I can import it from a config file, the all I have to do is modify that config file.

Is this possible in awk?

Thanks.
# 2  
Old 03-05-2013
Put your variables in some file, for example "vars.conf" defined as shell variables. Then create your awk scripts as follows:
Code:
#!/bin/bash
. vars.conf
awk -vLogFileHeader_StartServer=$LogFileHeader_StartServer -vsome_other_variable=$some_other_variable 'your
AWK
script'

# 3  
Old 03-05-2013
Note: some awks needs a space between -v and the awk variable and the shell variables need to be quoted:
Code:
awk -v awkvar1="$shellvar" ...

# 4  
Old 03-05-2013
Or use the variable behind your awk code
Code:
awk '{print var}' var="$shellvar" infile

# 5  
Old 03-05-2013
The way awk reads files makes it easier to have a config file like
Code:
var1 value1
var2 value2

You could use it like

Code:
awk 'BEGIN {
while((getline<"/path/to/config") > 0)
{ C[$1]=$2 } }

...'

Then you could use the variables like C['var1']
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 03-05-2013
If you put your awk code in program files and include them using the -f option, you can put the common code in a separate config file of awk commands and name it in the 1st -f option you pass to awk as in:
Code:
awk -f shared.awk -f application_specific.awk input_file...

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 03-06-2013
Quote:
Originally Posted by Corona688
The way awk reads files makes it easier to have a config file like
Code:
var1 value1
var2 value2

You could use it like

Code:
awk 'BEGIN {
while((getline<"/path/to/config") > 0)
{ C[$1]=$2 } }

...'

Then you could use the variables like C['var1']
Thanks!
I totally forgot that array can be used in such a case!

And thanks all other's help.
The -v is also good, just not suitable for my problem since I might have a long list of variables... @@
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Array - Export/Import in global environment variables.

Hello. During startup /etc/bash.bashrc.local generates some array ..... source /.../.../system_common_general_array_env_var ..... The file system_common_general_array_env_var contains : LEAP_VERSION='42.3' ARRAY_MAIN_REPO_LEAP=('zypper_local' 'openSUSE-Leap-'"$LEAP_VERSION"'-Non-Oss' ... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

How to put variables commands in config files?

Hi All, Seeking for your assistance on how to put in variables all the commands in /bin config files: /home/test/config_file/config.cfg cat /home/test/config_file/config.cfg ECHO=/bin/echo LS=/bin/lsMain script cat test.sh source=/home/test/config_file/config.cfg ECHO=$ECHO LS=$LS#i... (3 Replies)
Discussion started by: znesotomayor
3 Replies

3. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: gilberteu
6 Replies

4. Shell Programming and Scripting

PERL on windows accessing variables from a config file

Folks, I'm a perl moron, so please speak very slowly. : ) I'm modifying a build script that starts up an apache server. Now there is a .config file that hardcodes an old webserver path like this c:\oldWebserver. Now I don't want that hardcoded value, rather wish to use an... (3 Replies)
Discussion started by: MarkoRocko
3 Replies

5. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

6. Shell Programming and Scripting

Parse config file and store the values in variables

Hi, I have a config file that has blank, commented lines. I need to escape commented lines, blank lines, parse the remaining lines and store them in variables or array. the config file contains the following lines. # config file # Define Oracle User ORA_USER=abcde ORA_PASS=xyzabc... (8 Replies)
Discussion started by: Lakshmi Chowdam
8 Replies

7. UNIX for Dummies Questions & Answers

Issue with parsing config variables

I am using MKS tool kit on windows server. One config variable is defined in windows environment and I am trying to use that variable. # Below RootDir is defined in windows RootDir="\\f01\var" # in unix script details="$RootDir/src|$RootDir/tgt" src=`echo $details|awk -F '|' '{print... (1 Reply)
Discussion started by: madhukalyan
1 Replies

8. Shell Programming and Scripting

parsing config file to create new config files

Hi, I want to use a config file as the base file and parse over the values of country and city parameters in the config file and generate separate config files as explained below. I will be using the config file as mentioned below: (config.txt) country:a,b city:1,2 type:b1... (1 Reply)
Discussion started by: clazzic
1 Replies

9. Shell Programming and Scripting

how to access variables in a config file inside a shell script

I'm writing a shell script. I want to put the variables in a separate config files and use those inside my script. e.g. the config file (temp.conf)will have the values like mapping=123 file_name=xyz.txt I want to access these variables in temp.conf(i.e. mapping and file_name) from inside the... (7 Replies)
Discussion started by: badrimohanty
7 Replies

10. Shell Programming and Scripting

How to parse config variables from external file to shell script

How do i use a config.txt to recursively pass a set of variables to a shell script eg my config.txt looks like this : path=c://dataset/set1 v1= a.bin v2= b.bin path=c://dataset/set2 v1= xy.bin v2= abc.bin .................. and so on . and my testscript : (2 Replies)
Discussion started by: pradsh
2 Replies
Login or Register to Ask a Question