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 read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 03:05 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 05:57 AM
replace a column values with the first value in column sumeet UNIX for Advanced & Expert Users 3 02-06-2007 09:13 AM
Capturing the values of column in one parameter mgirinath Shell Programming and Scripting 5 06-14-2006 10:43 AM
Replace 10th column with a new column--- Terriblly hurry ahmedwaseem2000 Shell Programming and Scripting 2 09-05-2005 10:10 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-30-2007
Registered User
 

Join Date: Jul 2007
Posts: 3
Stumble this Post!
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
Forum Sponsor
  #2 (permalink)  
Old 07-30-2007
Registered User
 

Join Date: Jul 2007
Posts: 3
Stumble this Post!
How to pass one parameter from one column to another column 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

Expected Output
-----------------
< 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



#!/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;
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 put 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 to resolve this?So that I can get the output as I am expecting .The output should be as same as mentioned at the top as expected output.
Thanks in advance.

Regards,
Prasanta
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:58 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