Parsing a file and setting to variables.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing a file and setting to variables.
# 15  
Old 03-18-2017
Sourcing a file is not the only shell feature that is not supported in awk. If we assume that you have sourced settings for the following variables:
Code:
FIELD1="text1"
FIELD2="text2"

then the awk code:
Code:
  printf("<application name=%s$FIELD1%s>\n\t<field task=%supdate%s name=%s$FIELD2%s>\n", qq, qq,qq,qq,qq,qq)

will print:
Code:
<application name="$FIELD1">\n\t<field task="update" name="$FIELD2">

not:
Code:
<application name="text1">\n\t<field task="update" name="text2">

If the above is the output you want, you would need a printf call more like:
Code:
  printf("<application name=%s%s%s>\n\t<field task=%supdate%s name=%s%s%s>\n",
    qq, FIELD1, qq, qq, qq, qq, FIELD2, qq)

because variables and variables preceded by a $ are not expanded inside double-quoted strings in awk and in awk $var expands to the contents of the field number named by the number assigned to the variable named var; not to the string assigned to the variable named var.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing fields into variables

A record contains 50 fields separated by "~". I need to assign each of these fields to different variables. Following is the shell script approach I tried. RECORD="FIELD1~FIELD2~FIELD3~FIELD4~FIELD5~...........~FIELD50" VAR1=$(echo ${RECORD} | cut -d"~" -f 1) VAR2=$(echo ${RECORD} | cut... (5 Replies)
Discussion started by: krishmaths
5 Replies

2. Shell Programming and Scripting

Setting environment variables from a file :

Hi, I have around 10 environment variables in my shell script. i want to set this all in a file and just call that file in my shell script. How can i do that ? Please help. TIA! (6 Replies)
Discussion started by: qwertyu
6 Replies

3. UNIX for Dummies Questions & Answers

Parsing alphanumeric variables

Hi All, I have files with a column which has values and ranges, for example colA colB ERD1 3456 ERD2 ERD3 4456 I want to have the following output colA colB colC ERD1 3456 3456 ERD2 526887 526890 ERD3 4456 4456 Being a newbie to... (2 Replies)
Discussion started by: alpesh
2 Replies

4. Shell Programming and Scripting

Setting environment variables in Cron file

Hi, In Cron file i'm using username and password hard-coded and now i wann to use environmental veraiables in cron file. But Could you please guide me how to use these environmental variables in cron file ? Thanks, Shyamu.A (4 Replies)
Discussion started by: shyamu544
4 Replies

5. Shell Programming and Scripting

Perl: parsing variables

I have the following script: #!/usr/bin/perl -w @files = <*.csv>; foreach $file (@files) { open(FH, $file); my @dt = split(/_|.csv/, $file); while (<FH>) { chomp; print $dt . $dt . ",$_\n"; } close(FH); } This script reads in all csv files in the current directory... (2 Replies)
Discussion started by: figaro
2 Replies

6. UNIX for Dummies Questions & Answers

Setting up variables

Hi all, I have a shell script that sets up the environment for an application running on UNIX - ksh. This script is run using: . ./script_name XX where XX is a parameter. I want to run it from another shell script but when I do it I don't get the envornment variables set up and the prompt... (3 Replies)
Discussion started by: solar_ext
3 Replies

7. UNIX for Advanced & Expert Users

setting some variables

i have a file .NAMEexport MY_NAME=JOE when i do this at the command prompt #. .NAME $echo MY_NAME $JOEi created a script called Run.sh . .NAME At the command prompt i did #sh Run.sh #echo $MY_NAMEit returns nothing. What have i missed out? (7 Replies)
Discussion started by: new2ss
7 Replies

8. UNIX for Dummies Questions & Answers

Help - Setting variables equal to data from another file

Relatively new poster but long time reader. I tried searching all threads for similar situations with mine but I've had no luck with some of the solutions. I have a script (script.ksh) that is getting information from an Oracle DB and spooling it into a text file (log.txt). I would then like... (1 Reply)
Discussion started by: Crios121
1 Replies

9. Shell Programming and Scripting

Parsing and getting values of variables

suppose i have a file value where it returns 3 values a=1 b=2 c=4 when i run it. i am using this file in my shell script. how do i parse and get the value of a b and c? (3 Replies)
Discussion started by: Rekha
3 Replies

10. Shell Programming and Scripting

Setting variables in a function

I'm not quite sure what I'm doing wrong here. I've go several jobs which print reports. Occassionally a printer will break down and reports need to be move to another printer. Rather than hard code the printer names in our scripts I'm trying to set these programatically using our function... (1 Reply)
Discussion started by: BCarlson
1 Replies
Login or Register to Ask a Question