Reading from input file writing to output file 
Author Message
 Reading from input file writing to output file

I am looking for direction on a project I am working on.  I have a
file in the following format:

Program Name 1,10,10,10,10,10,10,10,10,10,10
Program Name 2,10,10,10,10,10,10,10,10,10,10

What I need to do is take this file and write to a new file in the
following format:

<PROGRAM NAME>Program Name 1</PROGRAM>
<RATING>10</RATING>
<RATING>10</RATING>

and so in.  The output file is in XML format.  That's the format I
need for the XML file.

I have some Unix books and O'Reilly's SED and AWK book, and have some
simple scripting experience.

Could someone please give me some direction on how to set up the
script to reformat the input file?

Thanks in advance.



Tue, 09 Dec 2003 13:30:39 GMT  
 Reading from input file writing to output file

Quote:

> I am looking for direction on a project I am working on.  I have a
> file in the following format:

> Program Name 1,10,10,10,10,10,10,10,10,10,10
> Program Name 2,10,10,10,10,10,10,10,10,10,10

> What I need to do is take this file and write to a new file in the
> following format:

> <PROGRAM NAME>Program Name 1</PROGRAM>
> <RATING>10</RATING>
> <RATING>10</RATING>

> and so in.  The output file is in XML format.  That's the format I
> need for the XML file.

> I have some Unix books and O'Reilly's SED and AWK book, and have some
> simple scripting experience.

> Could someone please give me some direction on how to set up the
> script to reformat the input file?

> Thanks in advance.

Try (one line):

'awk -F, '{print"<PROGRAM NAME>"$1"</PROGRAM>";i=2;do
{print"<RATING>"$i"</RATING>";i++}while(i<=NF)} infile'

Good luck

Michael Heiming



Tue, 09 Dec 2003 14:22:06 GMT  
 Reading from input file writing to output file


Quote:

>> I am looking for direction on a project I am working on.  I have a
>> file in the following format:

>> Program Name 1,10,10,10,10,10,10,10,10,10,10
>> Program Name 2,10,10,10,10,10,10,10,10,10,10

>> What I need to do is take this file and write to a new file in the
>> following format:

>> <PROGRAM NAME>Program Name 1</PROGRAM>
>> <RATING>10</RATING>
>> <RATING>10</RATING>

>> and so in.  The output file is in XML format.  That's the format I
>> need for the XML file.

>> I have some Unix books and O'Reilly's SED and AWK book, and have some
>> simple scripting experience.

>> Could someone please give me some direction on how to set up the
>> script to reformat the input file?

>> Thanks in advance.

>Try (one line):

>'awk -F, '{print"<PROGRAM NAME>"$1"</PROGRAM>";i=2;do
>{print"<RATING>"$i"</RATING>";i++}while(i<=NF)} infile'

personally, I prefer the for loop structure:

awk -F, '{print "<PROGRAM NAME>" $1 "</PROGRAM>"
          for(i=2;i<=NF;i++){
               print "<RATING>" $i "</RATING>"}}' infile

Chuck Demas

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Tue, 09 Dec 2003 15:19:34 GMT  
 Reading from input file writing to output file

Quote:



> > > I am looking for direction on a project I am working on.  I have a
> > > file in the following format:

> > > Program Name 1,10,10,10,10,10,10,10,10,10,10
> > > Program Name 2,10,10,10,10,10,10,10,10,10,10

> > > What I need to do is take this file and write to a new file in the
> > > following format:

> > > <PROGRAM NAME>Program Name 1</PROGRAM>
> > > <RATING>10</RATING>
> > > <RATING>10</RATING>

> > > and so in.  The output file is in XML format.  That's the format I
> > > need for the XML file.

> > > I have some Unix books and O'Reilly's SED and AWK book, and have some
> > > simple scripting experience.

> > > Could someone please give me some direction on how to set up the
> > > script to reformat the input file?

> > > Thanks in advance.

> > Try (one line):

> > 'awk -F, '{print"<PROGRAM NAME>"$1"</PROGRAM>";i=2;do
> > {print"<RATING>"$i"</RATING>";i++}while(i<=NF)} infile'

> personally, I prefer the for loop structure:

> awk -F, '{print "<PROGRAM NAME>" $1 "</PROGRAM>"
>           for(i=2;i<=NF;i++){
>           print "<RATING>" $i "</RATING>"}}' infile

Personally, I prefer thinking in lists, not loops:

    #!/usr/bin/perl -n
    chomp;

    print "<PROGRAM NAME>$progname</PROGRAM>\n";

Silly programmer! Loops are for kids! ;-)

--
Jim Monty

Tempe, Arizona USA



Wed, 10 Dec 2003 05:57:10 GMT  
 Reading from input file writing to output file


...

Quote:
>Personally, I prefer thinking in lists, not loops:

(Post that belongs in some other newsgroup - clipped)

Personally, I prefer paying attention to what newsgroup I am in, and
posting on topic.

Quote:
>Silly programmer! Loops are for kids! ;-)

But then again, some people think "Staying on topic is for other people".

The funny thing is this: Next time you (or anyone else) gets the urge to
post a P*** program in this newsgroup, ask yourself what kind of response
you'd get if you posted an AWK program in one of their newsgroups.

Believe me - I'm going to look like the nicest guy you can imagine,
compared to the reception you'll be getting there.



Wed, 10 Dec 2003 06:09:32 GMT  
 Reading from input file writing to output file

Quote:


...
>>personally, I prefer the for loop structure:

>>awk -F, '{print "<PROGRAM NAME>" $1 "</PROGRAM>"
>>          for(i=2;i<=NF;i++){
>>       print "<RATING>" $i "</RATING>"}}' infile

>Personally, I prefer thinking in lists, not loops:

>    #!/usr/bin/perl -n
>    chomp;

>    print "<PROGRAM NAME>$progname</PROGRAM>\n";

>Silly programmer! Loops are for kids! ;-)

Well if all you want to do is avoid looping constructs (but we know that's
not all you want to do, is it?), you could just use

awk '{ sub(/,/, "</PROGRAM>\n<RATING>")
gsub(/,/, "</RATING>\n<RATING>")
print "<PROGRAM NAME>" $0 "</RATING>" }' infile

And way OT, with all the other command line options perl provides, you'd
think there'd be one that made chomp unnecessary.



Wed, 17 Dec 2003 04:05:06 GMT  
 Reading from input file writing to output file

Quote:

> And way OT, ...

Thanks for the usual warning.

Quote:
> ... with all the other command line options perl provides, you'd
> think there'd be one that made chomp unnecessary.

    -l

See perldoc perlrun.

--
Jim Monty

Tempe, Arizona USA



Wed, 17 Dec 2003 06:45:36 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. reading input files and then output

2. newbie would like to break input file and output to separate files

3. sed: input file = output file

4. comparing an input file with an output file

5. Mutiple output files single Input file

6. Single file input ==> multi file output

7. with-input-from-file, with-output-to-file

8. with-input-from-file, with-output-to-file

9. Reading an array from file and outputting another file

10. Q: simple example of read file->convert char(s)->write file needed

11. Reading a binary file / writing binary data to a file

12. TEMPORARY FILE during input and output??

 

 
Powered by phpBB® Forum Software