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.
# 8  
Old 03-16-2017
Quote:
Originally Posted by jeffs42885
Hello all,

I ran into an issue with this in forming the XML.

I am calling the awk script as shown:

Code:
awk -f config/jeff.awk ./input/input.txt >> output.xml

I am getting this error:

Code:
 syntax error The source line is 1.
 The error context is
                 >>> . <<<  /location/to/properties/file/config.file
 awk: Quitting
 The source line is 1.

Because within the config.file I made some changes, not sure if there is a more efficient way to do this..

Code:
. /location/to/properties/file/config.file
BEGIN {
  FS=OFS=","
  qq="\""
  printf("<application name=%s$FIELD1%s>\n\t<field task=%supdate%s name=%s$FIELD2%s>\n", qq, qq,qq,qq,qq,qq)
}
{printf("\t<mapping db=%s%s%s displayed=%s%s%s />\n", qq, $1, qq, qq, $2, qq)}
END {
  printf("\t</field>\n</application>\n")
}

Am I doing this incorrectly?
No, this is incorrect.
What's in . /location/to/properties/file/config.file?
What are you trying to do?

Last edited by vgersh99; 03-16-2017 at 06:05 PM..
# 9  
Old 03-16-2017
Quote:
Originally Posted by vgersh99
No, this is incorrect.
What's in . /location/to/properties/file/config.file?
What are you trying to do?
variables..

VARIABLE1="something1"
VARIABLE2="something2"
# 10  
Old 03-16-2017
Quote:
Originally Posted by jeffs42885
variables..

VARIABLE1="something1"
VARIABLE2="something2"
huh? you lost me there.... What variables?
And what's inside ./input/input.txt?

Now I'm confused what's your input looks like.
# 11  
Old 03-16-2017
Post#7 is quite misleading. After rereading and rereading, the last code tagged block seems to be the config/jeff.awk script to be run by awk. The first line looks like it is meant to "source" /location/to/properties/file/config.file, which is a bash (or, generally, shell) builtin. Of course, awk doesn't understand this and errors out accordingly.
And, not sure why you think you need those variables - they're not used anywhere.
This User Gave Thanks to RudiC For This Post:
# 12  
Old 03-16-2017
Quote:
Originally Posted by RudiC
Post#7 is quite misleading. After rereading and rereading, the last code tagged block seems to be the config/jeff.awk script to be run by awk. The first line looks like it is meant to "source" /location/to/properties/file/config.file, which is a bash (or, generally, shell) builtin. Of course, awk doesn't understand this and errors out accordingly.
And, not sure why you think you need those variables - they're not used anywhere.
Apologies for this being misleading, allow me try and attempt to clarify.

You answered my question in this post. I am trying to source the config file just like I would do in a normal shell script. Obviously, that is one thing that is not working.

Code:
BEGIN {
  FS=OFS=","
  qq="\""
  printf("<application name=%s$FIELD1%s>\n\t<field task=%supdate%s name=%s$FIELD2%s>\n", qq, qq,qq,qq,qq,qq)
}
{printf("\t<mapping db=%s%s%s displayed=%s%s%s />\n", qq, $1, qq, qq, $2, qq)}
END {
  printf("\t</field>\n</application>\n")
}

When I remove the "sourcing" at the top of the file, this generates the XML like I expect it to, with the value of application name set as FIELD1, and the value of field name set as FIELD2.

It might have been clearer if I could code a config file to be read from this awk script, much like I have the shell script reading from. I hope this makes sense. Thank you for the clarification and any further assistance.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-17-2017 at 05:42 AM.. Reason: Added CODE tags.
# 13  
Old 03-17-2017
Sourcing configurations is something awk is not designed for. You can cheat like

Code:
awk -f config.awk  -f jeff.awk ./input/input.txt >> output.xml

with config.awk containing

Code:
{
VARIABLE1="something1"
VARIABLE2="something2"
}

or other methods presented in these fora like assigning variables outside the script, or using a data file to read the variables upfront (but losing variables' names).
# 14  
Old 03-17-2017
awk is not shell and does not understand shell syntax, awk is awk.

Code:
awk -f /location/to/properties/file/config.file -f config/jeff.awk ./input/input.txt >> output.txt

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