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 5 10 Hours Ago 11:29 AM
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 Search this Thread Display Modes
  #8  
Old 02-24-2006
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
Hi,

I cannot install it because I am not the system admin. And I am using a server from the company. I guess I can't use Ruby.
Reply With Quote
Forum Sponsor
  #9  
Old 02-24-2006
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
Hi,

I couldn't get the 2nd field into the output file. Can you please advise on how to add the 2nd field and so on?

Quote:
Originally Posted by vino
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
  #10  
Old 02-24-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
Show us what you have done so far.
Reply With Quote
  #11  
Old 02-24-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
Quote:
Originally Posted by vino
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.
Would be much better of as

Code:
IFS=","
while read conti country city id
do
CONT=$conti
COUNTRY=$country
CITY=$city
ID=$id
header $ID
done < input.file
Reply With Quote
  #12  
Old 02-24-2006
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
There are some changes in the requirements of the output file. One more fields needed.
There are also 2 more fields in the input file and the 2nd line is not needed.

This is what I have done:

Code:
#!/bin/ksh

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

BODY1='                <info1>'
BODY2='                <info2> </info2>
                <info3>'
BODY3='                <info4>'
BODY4='                <info5>0</info5>
                <info6> </info6>
                <info7> </info7>
                <info8>'
BODY5='                <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>'

INPUTFILE=/home/dir/input.csv
OUTPUTFILE=/home/dir/output.xml

function header {
	echo "$HEADER" > $OUTPUTFILE
	echo "$id" >> $OUTPUTFILE
	echo "\">" >> $OUTPUTFILE
}

function body1 {
	echo "$BODY1" >>$OUTPUTFILE
	echo "$continent" >>$OUTPUTFILE
	echo "</info1>" >>$OUTPUTFILE
}

function body2 {
	echo "$BODY2" >>$OUTPUTFILE
	echo "$country" >>$OUTPUTFILE
	echo "</info3>" >>$OUTPUTFILE
}

function body3 {
	echo "$BODY3" >>$OUTPUTFILE
	echo "$city" >>$OUTPUTFILE
	echo "</info4>" >>$OUTPUTFILE
}

function body4 {
	echo "$BODY4" >>$OUTPUTFILE
	echo "$date" >>$OUTPUTFILE
	echo "</info8>" >>$OUTPUTFILE
}

function body5 {
	echo "$BODY5" >>$OUTPUTFILE
}


IFS=;
while read continent country city id address date
do
	ID=$id
	CONTINENT=$continent
	COUNTRY=$country
	CITY=$city
             DATE=$date
	header $ID
	body1 $CONTINENT
	body2 $COUNTRY
	body3 $CITY
	body4 $DATE
	body5
done < $INPUTFILE
The input file contents:
Code:
continent;country;city,id;address;date
---------;-----------;----------------;----------;---------;--------
asia;japan;tokyo;123;apple road;12012000
europe;germany;munich;456;orange street;13072001
africa;eygpt;cairo;789;banana lane;06121999
Reply With Quote
  #13  
Old 02-24-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
In

Code:
while read continent country city id address date
do
	ID=$id
	CONTINENT=$continent
	COUNTRY=$country
	CITY=$city
             DATE=$date
	header $ID
done < $INPUTFILE
do you know what header $ID means ?

You are invoking the function header with $ID as a parameter.

So inside the header function, this parameter $ID is accessible using the construct $@ or $1 ( anyway you like...)

So in your case, the header function has no idea what $id is. Hence it should be

Code:
function header {
	echo "$HEADER" > $OUTPUTFILE
	echo "$@" >> $OUTPUTFILE
	echo "\">" >> $OUTPUTFILE
}
Likewise, for all the other functions, you should follow suit.
Reply With Quote
  #14  
Old 02-24-2006
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
Hi,

I have tried with
Code:
function header {
	echo "$HEADER" > $OUTPUTFILE
	echo "$@" >> $OUTPUTFILE
	echo "\">" >> $OUTPUTFILE
}
but the parameters are not parsed from the input to the ouput file. I have the format of the output file without all the parameters.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:57 PM.


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

Content Relevant URLs by vBSEO 3.2.0