The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to view the contents of .gz file without extracting the file? amitkhiare Shell Programming and Scripting 3 01-13-2008 08:35 PM
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 07:17 PM
append file name contents to a target file gopskrish UNIX for Dummies Questions & Answers 2 10-27-2006 03:18 AM
Reading specific contents from a file and appending it to another file dnicky Shell Programming and Scripting 5 10-04-2005 02:45 AM
ls contents of a file douknownam Shell Programming and Scripting 7 06-14-2004 06:29 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-23-2006
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
Stumble this Post!
Creating file contents using contents of another file

Hi,

I am not sure how to start doing this so I hope to get some advice as to how to start.

I have 2 files. The source file contains data that I needed is in columns delimited by ";". For example, in this format:

Code:
"CONTINENT","COUNTRY","CITY","ID"
"asia","japan","tokyo","123"
"europe","germany","munich","456"
"africa","eygpt","cairo","789"
The output format is in xml format and the contents is something like:

Code:
<?xml version="1.0"?>
<!DOCTYPE ALR [
        <!ENTITY gt "&amp;gt;">
        <!ENTITY lt "&amp;lt;">
        <!ENTITY quot "&amp;quot;">
]>
<datafile>
        <info ID="#id should be here#">
                <info1>#continent name should be here'</info1>
                <info2> </info2>
                <info3>#country name should be here#</info3>
                <info4>#city name should be here#</info4>
                <info5>some fixed data</info5>
                <info6> </info6>
                <info7> </info7>
                <info8>some fixed data</info8>
                <info9>some fixed data</info9>
                <info10>some fixed data</info10>
                <info11>some fixed data</info11>
        </info>
        <action>
                <attributes>
                        <attributkey id="011" >N</attributkey>
                        <attributkey id="106" >N</attributkey>
                        <attributkey id="114" >N</attributkey>
                        <attributkey id="119" >N</attributkey>
                                        </attributes>
                <comment> </comment>
        </action>
</datafile>
There should be one output file created for each line in the source file.

Anyone can help? Thanks a lot!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-23-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,659
Stumble this Post!
See the solution in Want to show files on web page

You need to do something similiar.

Something like this


Code:
HEADER='<?xml version="1.0"?>
<!DOCTYPE ALR [
        <!ENTITY gt "&amp;gt;">
        <!ENTITY lt "&amp;lt;">
        <!ENTITY quot "&amp;quot;">
]>
<datafile>
        <info ID="'

function header {

echo "$HEADER" > output.xml
echo "$1" >> output.xml
echo "\">" >> output.xml

}
Parse the country specific details as

Code:
while read line
do
CONTINENT=$(echo "$line" | awk -F, '{ print $1 }')
COUNTRY=$(echo "$line" | awk -F, '{ print $2 }')
.
.

header $ID
done < input.file
where header is the method given above.
Reply With Quote
  #3 (permalink)  
Old 02-23-2006
Registered User
 

Join Date: Jul 2005
Posts: 137
Stumble this Post!
Use Ruby:

Code:
$out_count = 0

def process( str )

  str.strip!
  continent,country,city,id = str[1..-2].split(/","/)

  $out_count += 1
  File.open( "output_#{ $out_count }.xml", "w" ){|handle|

    handle.puts <<"HERE_DOC"
<?xml version="1.0"?>
<!DOCTYPE ALR [
        <!ENTITY gt "&amp;gt;">
        <!ENTITY lt "&amp;lt;">
        <!ENTITY quot "&amp;quot;">
]>
<datafile>
        <info ID="#{ id }">
                <info1>#{ continent }</info1>
                <info2> </info2>
                <info3>#{ country }</info3>
                <info4>#{ city }</info4>
                <info5>some fixed data</info5>
                <info6> </info6>
                <info7> </info7>
                <info8>some fixed data</info8>
                <info9>some fixed data</info9>
                <info10>some fixed data</info10>
                <info11>some fixed data</info11>
        </info>
        <action>
                <attributes>
                        <attributkey id="011" >N</attributkey>
                        <attributkey id="106" >N</attributkey>
                        <attributkey id="114" >N</attributkey>
                        <attributkey id="119" >N</attributkey>
                </attributes>
                <comment> </comment>
        </action>
</datafile>
HERE_DOC

  }
end


# Discard 1st line of file.
gets

# Read rest of file.
while gets
  process( $_ )
end
Save this code in file "make_xml.rb".
Run it with
ruby make_xml.rb my_input_file

The output files are named "output_1.xml", etc.
Reply With Quote
  #4 (permalink)  
Old 02-24-2006
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
Stumble this Post!
What is ruby command? When I tried to enter the command line you have written, it says "ruby:command not found"
Reply With Quote
  #5 (permalink)  
Old 02-24-2006
Registered User
 

Join Date: Jul 2005
Posts: 137
Stumble this Post!
Ruby is a free programming language.
Reply With Quote
  #6 (permalink)  
Old 02-24-2006
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
Stumble this Post!
I can't seem to use it in my unix interface. What should I do?

Quote:
Originally Posted by futurelet
Ruby is a free programming language.
Reply With Quote
  #7 (permalink)  
Old 02-24-2006
Registered User
 

Join Date: Jul 2005
Posts: 137
Stumble this Post!
It's readily available and open source. Could you download and install it?

Try this link: http://www.ruby-lang.org/en/20020102.html
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:39 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0