![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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" Code:
<?xml version="1.0"?>
<!DOCTYPE ALR [
<!ENTITY gt "&gt;">
<!ENTITY lt "&lt;">
<!ENTITY quot "&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>
Anyone can help? Thanks a lot! |
| Forum Sponsor | ||
|
|
|
||||
|
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 "&gt;">
<!ENTITY lt "&lt;">
<!ENTITY quot "&quot;">
]>
<datafile>
<info ID="'
function header {
echo "$HEADER" > output.xml
echo "$1" >> output.xml
echo "\">" >> output.xml
}
Code:
while read line
do
CONTINENT=$(echo "$line" | awk -F, '{ print $1 }')
COUNTRY=$(echo "$line" | awk -F, '{ print $2 }')
.
.
header $ID
done < input.file
|
|
|||
|
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 "&gt;">
<!ENTITY lt "&lt;">
<!ENTITY quot "&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
Run it with ruby make_xml.rb my_input_file The output files are named "output_1.xml", etc. |
|
|||
|
It's readily available and open source. Could you download and install it?
Try this link: http://www.ruby-lang.org/en/20020102.html |
|||
| Google The UNIX and Linux Forums |