Sponsored Content
Top Forums Shell Programming and Scripting Converting rows to columns in csv file Post 302749005 by Fundix on Friday 28th of December 2012 04:38:09 AM
Old 12-28-2012
My solution (I need to practice awk Smilie)

file
Code:
c1,c2,C3,c4,c5,c6
r1,r2,r3,r4,r5,r6
p1,p2,p3,p4,p5,p6
w1,w2,w3,w4,w5,w6
y1,y2,y3,y4,y5,y6
z1,z2,z3,z4,z5,z6

script.awk
Code:
BEGIN {
  FS=","
}

{
  for (i = 1; i <= NF; i++)
      arr[i,NR]=$i
}

END {
  for (x = 1; x <= NR; x++)
  {
    for (y = 1; y <= NF; y++)
      {
        printf("%s", arr[x,y])
        if(y < NF)
          printf("%s", ",")
      }
    print ""
  }
} # END

Execution :
Code:
#awk -f script.awk < file

result
Code:
c1,r1,p1,w1,y1,z1
c2,r2,p2,w2,y2,z2
C3,r3,p3,w3,y3,z3
c4,r4,p4,w4,y4,z4
c5,r5,p5,w5,y5,z5
c6,r6,p6,w6,y6,z6

It should work for a more than 6x6 matrix

---------- Post updated 28th Dec 2012 at 10:38 AM ---------- Previous update was 27th Dec 2012 at 03:35 PM ----------

A great majority of the scripts at my new office are written in KSh, but some Perl scripts remain in some servers.
I need to practice Perl too, so hereafter the Perl version of the previous awk solution :
Code:
#!/usr/bin/perl -w
use strict;
 
my $cur_dir = $ENV{PWD};
my $filename = $cur_dir."/file";
 
open(FILE,"<$filename") or die"open: $!";
my ($record,@fields,%col);
while( defined( $record = <FILE> ) ) {
  my $col_counter=1;
  chomp $record;
  @fields=split(/,/,$record);
  foreach (@fields) {
    $col{$col_counter} .= "$_,";
    $col_counter++;
  }
}
 
close(FILE);
 
for ( my $i=1; $i<=keys(%col); $i++ ) {
  my $line=$col{$i};
  chop $line;
  print "$line\n";
}

If Awk of Perl gurus have comments about my code, they are welcome Smilie.

I'm also wondering what is the best choice for my future new scripts : KSh+awk or Perl ?

Thank You
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

deleting rows & columns form a csv file

Hi , I want to delete some rows & columns from file. can someone please help me on this? Regards. (2 Replies)
Discussion started by: code19
2 Replies

2. Shell Programming and Scripting

how to converting rows to columns, bash

I have in file these words: @fraza1 = rw @fraza2 = r @fraza3 = r @fraza4 = r @fraza5 = r @fraza1 = r @fraza6 = r @fraza7 = r @fraza2 = r @fraza8 = r @fraza9 = r ... I would like so that: ,rw,@fraza1 ,r,@fraza2 (2 Replies)
Discussion started by: patrykxes
2 Replies

3. UNIX for Dummies Questions & Answers

Converting columns into rows

Is there anyway to convert columns into raws using awk? (or any other command line):eek::eek::eek::eek::eek::eek::eek::eek::eek: (1 Reply)
Discussion started by: cosmologist
1 Replies

4. Shell Programming and Scripting

converting rows into columns

Hi, I am trying to fetch some values from db and spooling the output to a file. when i query the db for the values, i get the values in following format. PC_1 wf_test1 Test PC_2 wf_test2 Test PC_3 wf_test3 Test But my spool file was created in following format. PC_1 wf_test1 Test... (20 Replies)
Discussion started by: svajhala
20 Replies

5. Shell Programming and Scripting

Converting rows to columns using shell script

I have a script which converts rows to columns. file_name=$1 mailid=$2 #CREATE BACKUP OF ORIGINAL FILE #cp ${file_name}.xlsx ${file_name}_temp.xlsx #tr '\t' '|' < ${file_name}_temp.xlsx > ${file_name}_temp.csv #rm ${file_name}_temp.xlsx pivot_row=`head -1 ${file_name}` sed 1d... (3 Replies)
Discussion started by: andy538
3 Replies

6. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. UNIX for Dummies Questions & Answers

Converting Columns To Rows Sort Of

Hi I'm a UNIX awk and sed novice at best. I'm trying to creat a .csv file so it can be graphed in Excel. Tried various xargs, awk, sed and paste but just can't seem to get the data to line up. Not sure if this is beyond for a question in these forums. Any help would greatly be appreciated. Have... (4 Replies)
Discussion started by: jimmyf
4 Replies

8. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

9. Shell Programming and Scripting

Converting rows into columns

hi all I need a help ..I have a script that takes has command and its output is like below.. a b a v v a c I am assigning the above outputs to a variable .. <variable name> = <command output> problem here is when I echo the variable ..it gives me output like " a b... (3 Replies)
Discussion started by: shankarb
3 Replies

10. Shell Programming and Scripting

Converting columns of text to rows, with blank lines

I've spent the past hour trying different things and googling for this solution and cannot find the answer. Found variations of this, but not this exact thing. I have the following text, which is the output from our mainframe. Each field is on a separate line, with a blank line between each... (7 Replies)
Discussion started by: lupin..the..3rd
7 Replies
RESIZE(1)						      General Commands Manual							 RESIZE(1)

NAME
resize - set environment and terminal settings to current xterm window size SYNOPSIS
resize [ -u | -c ] [ -s [ row col ] ] DESCRIPTION
Resize prints a shell command for setting the appropriate environment variables to indicate the current size of xterm window from which the command is run. For this output to take effect, resize must either be evaluated as part of the command line (usually done with a shell alias or function) or else redirected to a file which can then be read in. From the C shell (usually known as /bin/csh), the following alias could be defined in the user's .cshrc: % alias rs 'set noglob; eval `resize`' After resizing the window, the user would type: % rs Users of versions of the Bourne shell (usually known as /bin/sh) that don't have command functions will need to send the output to a tempo- rary file and then read it back in with the "." command: $ resize > /tmp/out $ . /tmp/out Resize determines the user's current shell by first checking if $SHELL is set, and using that. Otherwise it determines the user's shell by looking in the password file. Generally Bourne-shell variants (including ksh) do not modify $SHELL, so it is possible for resize to be confused if one runs resize from a Bourne shell spawned from a C shell. OPTIONS
The following options may be used with resize: -u This option indicates that Bourne shell commands should be generated even if the user's current shell isn't /bin/sh. -c This option indicates that C shell commands should be generated even if the user's current shell isn't /bin/csh. -s [rows columns] This option indicates that Sun console escape sequences will be used instead of the VT100-style xterm escape codes. If rows and columns are given, resize will ask the xterm to resize itself. However, the window manager may choose to disallow the change. Note that the Sun console escape sequences are recognized by XFree86 xterm and by dtterm. The resize program may be installed as sunsize, which causes makes it assume the -s option. The rows and columns arguments must appear last; though they are normally associated with the -s option, they are parsed separately. FILES
/etc/termcap for the base termcap entry to modify. ~/.cshrc user's alias for the command. ENVIRONMENT
TERM set to "xterm" if not already set. TERMCAP variable set on systems using termcap COLUMNS, LINES variables set on systems using terminfo SEE ALSO
csh(1), tset(1), xterm(1) AUTHORS
Mark Vandevoorde (MIT-Athena), Edward Moy (Berkeley) Copyright (c) 1984, 1985 by X Consortium See X(7) for a complete copyright notice. X Window System RESIZE(1)
All times are GMT -4. The time now is 01:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy