The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 10 12-18-2008 05:59 AM
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 10:17 PM
append file name contents to a target file gopskrish UNIX for Dummies Questions & Answers 2 10-27-2006 06:18 AM
Reading specific contents from a file and appending it to another file dnicky Shell Programming and Scripting 5 10-04-2005 05:45 AM
ls contents of a file douknownam Shell Programming and Scripting 7 06-14-2004 09:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-23-2006
ReV ReV is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 34
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!
  #2 (permalink)  
Old 02-23-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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.
  #3 (permalink)  
Old 02-23-2006
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
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.
  #4 (permalink)  
Old 02-24-2006
ReV ReV is offline
Registered User
  
 

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

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

Join Date: Jun 2005
Posts: 34
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.
  #7 (permalink)  
Old 02-24-2006
futurelet futurelet is offline
Registered User
  
 

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

Try this link: http://www.ruby-lang.org/en/20020102.html
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0