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.
# 1  
Old 03-15-2017
Parsing a file and setting to variables.

greetings all, I have a task right now that is somewhat stumping me, and I am not sure what the best approach is to take it.

I have a text file that will contain something similar to the following:

Code:
 
 first1, other1
 first2, other2
 first3, other3
 first4, other4

I have to generate an XML file that takes the value of first and other, and puts them into an XML file such as below so I can run my program against it:

Code:
 
 <application="testApp">
                <task="modify" name="document">
                <mapping first="first1" other="other1" />
                <mapping first="first2" other="other2" />
                <mapping first="first3" other="other3" />
                <mapping first="first4" other="other4" />
                 </task>
 </application>

Scratching my head over this one. Whats the best approach at this?

Thanks in advance.
# 2  
Old 03-15-2017
something along these lines: awk -f jeff.awk myTextFile.txt where jeff.awk is:

Code:
BEGIN {
  FS=OFS=","
  qq="\""
  printf("<application=%stestApp%s>\n\t<task=%smodify%s name=%sdocument%s>\n", qq, qq,qq,qq,qq,qq)
}
{printf("\t<mapping first=%s%s%s other=%s%s%s />\n", qq, $1, qq, qq, $2, qq)}

END {
  printf("\t</task>\n</application>\n")
}

# 3  
Old 03-15-2017
This looks like it works. A bit more advanced than I had thought. One last question, is how do I output this to a file. I am trying basic redirect of

Code:
>> xml.out

and it does not like that.
# 4  
Old 03-15-2017
Quote:
Originally Posted by jeffs42885
This looks like it works. A bit more advanced than I had thought. One last question, is how do I output this to a file. I am trying basic redirect of

Code:
>> xml.out

and it does not like that.

awk -f jeff.awk myTextFile.txt > xml.out
# 5  
Old 03-15-2017
If you're expecting to append to the file, xml doesn't really work that way. A new file would have to be generated each time.
# 6  
Old 03-15-2017
wow, that was a newb error!!!! you folks rock!
# 7  
Old 03-16-2017
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?
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