Hi,
I have a small requirement in chainging the rows to columns. The below example.txt contains info as shown
Name:Person1
Age:30
Name:Person2
Age:40
Name:Person3
Age:50
I want to make it displayed as hown below
Name:Person1 Age:30
Name:person2 Age:40
Name:Person3 Age:50
I tried by doing the 'tr' command by chaning all new lines to carriage -return it dowsnt work.
i.e. cat example.txt|egrep -i 'Name|Age'|tr '\n' '\t\r' -- it doesnt serve me bcoz the \t is considered but not \r............Please some one help me
awk '{a=$0;getline;print a, $0}' file
Hi
how should i have to use incase i like to use printf statement for formmating the text displayed ........
some thing like...........
printf "%-20s,%-10s" a, $0 ...could u pls help me
hi,
You may use below perl version.
It can transpose any matrix.
Two parameter, 1>filename, 2>delimeter
package Utl;
BEGIN {
use Exporter();
@ISA = qw(Exporter);
@EXPORT = qw (&ChangeMetrix);
}
sub ChangeMetrix
{
if ($#_<1){
print "Usage: ChangeMetrix filename delimeter\n";
exit;
}
$file=shift;
$del=shift;
open(FH,"<$file");
while(<FH>){
$_=~tr/\n//d;
@arr=split($del,$_);
$col=$#arr;
for($i=0;$i<=$#arr;$i++){
$index=sprintf("%s%s",$.,$i);
$hash{$index}=$arr[$i];
}
$row=$.;
}
close(FH);
for($i=0;$i<=$col;$i++){
for($j=1;$j<=$row;$j++){
$temp=sprintf("%s%s",$j,$i);
print $hash{$temp},$del;
}
print "\n";
}
}
return 1;
or you can use below easy one to meet your ques:
cat file | paste - -