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
PHP arrays in arrays JerryHone Web Programming, Web 2.0 and Mashups 3 03-13-2009 02:19 PM
awk arrays can do this better - but how? littleIdiot Shell Programming and Scripting 1 02-03-2009 05:13 AM
Need Help with awk and arrays fusionX Shell Programming and Scripting 7 02-11-2008 06:41 PM
arrays in awk??? craigsky Shell Programming and Scripting 3 08-27-2007 09:13 PM
KSH and arrays whited05 Shell Programming and Scripting 1 06-24-2005 12:07 PM

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 04-17-2009
james2009 james2009 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 5
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

Last edited by james2009; 04-17-2009 at 08:34 AM..
  #2 (permalink)  
Old 04-17-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
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
  #3 (permalink)  
Old 04-17-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
@panyam, wrong output

@OP, here's a Perl solution
Code:
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
Code:
# ./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"
  #4 (permalink)  
Old 04-17-2009
james2009 james2009 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 5
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
  #5 (permalink)  
Old 04-17-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Ohh ...I am sorry i did not check OP properly...
  #6 (permalink)  
Old 04-17-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
James the solution was already present in the forum . I just googled it.

Row to column transpose

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

Last edited by panyam; 04-17-2009 at 05:42 AM..
  #7 (permalink)  
Old 04-17-2009
james2009 james2009 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 5
Thanks for your solutions. I have updated the initial question. Any solutions?
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 11:34 AM.


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