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 read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 07: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 09:57 AM
replace a column values with the first value in column sumeet UNIX for Advanced & Expert Users 3 02-06-2007 01:13 PM
Capturing the values of column in one parameter mgirinath Shell Programming and Scripting 5 06-14-2006 01:43 PM
Replace 10th column with a new column--- Terriblly hurry ahmedwaseem2000 Shell Programming and Scripting 2 09-06-2005 01:10 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 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 05:20 AM.. Reason: add code tags
  #2 (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 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
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 02:23 PM.


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