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
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 06:20 AM..
Reason: add code tags
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.
Example:
I have files in below format
file 1:
zxc,133,joe@example.com
cst,222,xyz@example1.com
File 2 Contains:
hxd
hcd
jws
zxc
cst
File 1 has 50000 lines and file 2 has around 30000 lines :
Expected Output has to be :
hxd
hcd
jws (5 Replies)
Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2
Scenario 1
i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Hi Experts,
Please bear with me, i need help
I am learning AWk and stuck up in one issue.
First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique.
Second point : For... (1 Reply)
Hello, I have a script extracting columns of useful numbers from a data file, and manipulating the numbers with awk commands. I have problems with my script...
1. There are two lines assigning numbers to $BaseForAveraging. If I use the commented line (the first one) and let the second one... (9 Replies)
Hi All,
I have a file test.txt.
Content of test.txt :
1 vinay se
2 kumar sse
4 kishore tl
I am extracting the content of file with below command.
awk '$2 ~ "vinay" {print $0}' test.txt
Now instead of hardcoding $2 is there any way pass $2 as variable and compare with a... (7 Replies)
Hi,
I have a similar input format-
A_1 2
B_0 4
A_1 1
B_2 5
A_4 1
and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks!
letter number_of_letters... (5 Replies)
Hi,
My input files is like this
axis1 0 1 10
axis2 0 1 5
axis1 1 2 -4
axis2 2 3 -3
axis1 3 4 5
axis2 3 4 -1
axis1 4 5 -6
axis2 4 5 1
Now, these are my following tasks
1. Print a first column for every two rows that has the same value followed by a string.
2. Match on the... (3 Replies)
Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately.
Here's some example output:
Iface RxMBytes RxPackets RxErrs RxDrop TxMBytes TxPackets TxErrs TxDrop
bond0 9 83830... (3 Replies)
Hi,
I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column.
clientname,day-of-month,max-users
-----------------------------------
client1,20120610,5
client2,20120610,2
client3,20120610,7... (3 Replies)
Hello,
I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2
SDF4 -0.211654
SDF4 0.978068
... (1 Reply)