arrays in awk

Hi,

I have the following data in a file for example:

Name="Fred","Bob","Peterson","Susan","Weseley"
Age="24","30","28","23","45"
Study="English","Engineering","Physics","Maths","Psychology"
Code="0","0","1","1","0"
Name="Fred2","Bob2","Peterson2","Susan2","Weseley2"
Age="23","29","25","21","44"
Study="English2","Engineering2","Physics2","Maths2","Psychology2"
Code="1","1","1","1","1"

Can you help me with an awk script to print the file such as:

Name,Age,Study,Code
Fred,24,English,0
Bob,30,Engineering,0
Peterson,28,Physics,1
Susan,23,Maths,1
Weseley,45,Psychology,0
Fred2,23,English2,1
Bob2,29,Engineering2,1
.
.
.

Thanks

TESTBOX> sed -e 's/=/,/' -e 's/"//g' inp.txt

Name,Fred,Bob,Peterson,Susan,Weseley
Age,24,30,28,23,45
Study,English,Engineering,Physics,Maths,Psychology
Code,0,0,1,1,0

@panyam, wrong output

@OP, here's a Perl solution

my $file="file";
my @array;
open(IF,"<",$file) or die "Cannot open input file:$!\n";
while(my $line = <IF>){
 chomp($line);
 push(@array,[split/=|,/,$line]); 
}
for $i ( 0 .. $#{$array[0]} ) {
    for $j ( 0 .. $#array ) {
        if( $j==$#array){
            print "$array[$j][$i]\n";
        }else{
            print "$array[$j][$i] ";   
        }
    }
}

output

# ./test.pl
Name Age Study Code
"Fred" "24" "English" "0"
"Bob" "30" "Engineering" "0"
"Peterson" "28" "Physics" "1"
"Susan" "23" "Maths" "1"
"Weseley" "45" "Psychology" "0"

panyam,

Your script only removes the double quotes and does not format it correctly. I need the output to be like this

Name,Age,Study,Code
Fred,24,English,0
Bob,30,Engineering,0
Peterson,28,Physics,1

Ohh ...I am sorry i did not check OP properly...

open $fh,"<","a.spl";
my (%hash,%h,$cnt);
while(<$fh>){
	chomp;
  my @arr=split("=",$_);
  my @tmp=split(",",$arr[1]);
  map {s/"//g} @tmp;
  $hash{$arr[0]}=\@tmp;
  $cnt=$#tmp if $cnt==0;
  $h{$arr[0]}=$.;
}
my @brr=sort {$h{$a} <=> $h{$b}} keys %hash;
print join ",",@brr;
print "\n";
for(my $i=0;$i<=$cnt;$i++){
	foreach $k (@brr){
		print $hash{$k}->[$i],",";
	}
	print "\n";
}

James the solution was already present in the forum . I just googled it.

I hope the given shell script ( in the URL) work fine.Before running the script make sure that input file is a comma seperated file.

this is wht the op i got

avalon:/disk1/jvsh/TEST>awk -f testawk.awk 1.txt
Name Age Study Code
Fred 24 English 0
Bob 30 Engineering 0
Peterson 28 Physics 1
Susan 23 Maths 1
Weseley 45 Psychology 0

Thanks for your solutions. I have updated the initial question. Any solutions?

try this:
awkfile as below:
BEGIN{FS="="}
{
gsub(/"/,"")
}
$1 ~ /Name/{name = name ","$2}
$1 ~ /Age/{age = age ","$2}
$1 ~ /Study/{study=study ","$2}
$1 ~ /Code/{code = code ","$2}
END{
split(name,name1,",")
split(age,age1,",")
split(study,study1,",")
split(code,code1,",")
#for (i in name1)
size=length(name1)
#print size
print "Name,Age,Study,Code"
for (i=2;i<=size;i++)
printf("%s,%s,%s,%s\n",name1[i],age1[i],study1[i],code1[i])
}

awk -f awkfile filename

cheers,
Devaraj

Hello devtakh,

The script is not working

How are you running it..and what is the error?

what is name1,age1,...?..

There it will throw the error.

name1, age1 ect are arrays that hold the values of the specific strings

try this

BEGIN{FS="="}
{
gsub(/"/,"")
}
$1 ~ /Name/{name = name ","$2}
$1 ~ /Age/{age = age ","$2}
$1 ~ /Study/{study=study ","$2}
$1 ~ /Code/{code = code ","$2}
END{
split(name,name1,",")
split(age,age1,",")
split(study,study1,",")
split(code,code1,",")
for (i in name1)size ++
print "Name,Age,Study,Code"
for (i=2;i<=size;i++)
printf("%s,%s,%s,%s\n",name1[i],age1[i],study1[i],code1[i])
}

cheers,
Devaraj Takhellambam

Good dev,

It's working Fine :slight_smile: :).

Thanks devtakh, it works but could that be done in a one-liner using multidimensional arrays?