The UNIX and Linux Forums  

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



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 07-30-2007
prasanta jena prasanta jena is offline
Registered User
 

Join Date: Jul 2007
Posts: 3
How to pass one parameter from one column to another in AWK

I am having scenario where
input file is containing text like below
-----------------------------------
Name,x.y.z.i.j.k,p.q.r.s.t.k,y.d.f.u.g.h,e.r.t.yu.t.u,......
Place,Bangalore,hyderabad,Chennai,Delhi,............


I need to read and put it in file in a column structure
like
< Mapping>
Name = x.y.z.i.j.k (First field)
Place = bangalore 4th place of name field = i (i out of x.y.z.i.j.k)
</Mapping>

< Mapping>
Name = p.q.r.s.t.k (First field)
Place = Hyderabad 4th place of name field = s (s out of p.q.r.s.t.k)
</Mapping>

< Mapping>
Name = y.d.f.u.g.h (First field)
Place = Chennai 4th place of name field = u (u out of y.d.f.u.g.h)
</Mapping>

< Mapping>
Name = e.r.t.yu.t.u (First field)
Place = Delhi 4th place of name field = yu (yu out of e.r.t.yu.t.u)
</Mapping>
...................
...................

I have written one shell script for that which is mentioned below


Code:
   #!/bin/sh
usage ()
{
    echo
    echo "Usage : csvtoxml.sh P"
    echo
    echo "where P = CSVFileName that you want to convert."
    echo "This  will be used to convert csv file to xml file"
    echo
    exit
}
## if the number of parametes are less than 1
##
if [ $# != 1 ]
then
    usage
    exit
fi
## Check for file
##
if [ ! -e $1 ]
then
    echo "File $1 does not exist"
    exit
fi
## Remove outfile if it is already exist
##
if [ -e Results.xml ]
then
    rm Results.xml
fi
val="start"
echo $val
tr -d '\r' < $1 > TempUnixFile
ColumnNum="2"
TotalColumn=`head -1 TempUnixFile | tr -cd ',' | wc -c`
TotalColumn=`expr $TotalColumn + 1`
while [ "$ColumnNum" -le "$TotalColumn" ]
do
 cut -d',' -f"$ColumnNum" TempUnixFile |
awk -F"," 'BEGIN { print "<Mapping>"}
                  {if ( NR == 1 )
                   print "\t<FameName name=\""""$1"""" " riskclass=\"""""MGI" "\"""" "/>";
                   line=$1;
                   oldifs="$IFS"
                   IFS="."
                   set "$line"
                   val=$4;
                   print "\t<FameName name=\""""$4"""" " riskclass=\"""""MGI" "\"""" "/>";
                   FS=",";
                   }
                   {if ( NR == 2 )print "\t<EdgeName Stem=\""""$1 "\"""" " Ref1=\""""$val "\"""" " Ref2=\"""""" "\"""" " IsScenario=\"""""" "\"""" " IsLevel=\"""""" "\"""" " IsCcyImpVol=\"""""" "\"""" " IsAstra=\"""""Yes" "\""""  "/>"}
                    END { print "</Mapping>" }
                  ' >> Results.xml
    ColumnNum=`expr $ColumnNum + 1`

done
rm -f TempUnixFile


But the out out comes like
----------------------------

< Mapping>
Name = x.y.z.i.j.k (First field)
Place = bangalore 4th place of name field =
</Mapping>

< Mapping>
Name = p.q.r.s.t.k (First field)
Place = Hyderabad 4th place of name field =
</Mapping>

< Mapping>
Name = y.d.f.u.g.h (First field)
Place = Chennai 4th place of name field =
</Mapping>

< Mapping>
Name = e.r.t.yu.t.u (First field)
Place = Delhi 4th place of name field =
</Mapping>
....................
....................


Can some body please help me?
Thanks in advance.

Regards,
Prasanta

Last edited by blowtorch; 07-30-2007 at 02:20 AM.. Reason: add code tags
Reply With Quote
Remove advertisements
!!
Forum Sponsor