You are not logged in.

#1 2021-06-30 21:58:35

SupKurtJ
Member
Registered: 2015-07-18
Posts: 34

[SOLVED] Bash script swap colums and rows from .csv

Hi,

I have a file.csv like this :

A;B;C;D;E;F;G;H;I;J;K
1;2;3;4;5;6;7;8;9;10;11
aa;bb;cc;dd;ee;ff;gg;hh;ii;jj;kk

And I want to convert it to :
A;1;aa
B;2;bb
C:3:cc

Etc....

Do you have any idea please ?

Thanks smile

Last edited by SupKurtJ (2021-06-30 22:36:00)

Offline

#2 2021-06-30 22:07:57

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 17,852

Re: [SOLVED] Bash script swap colums and rows from .csv

This does not address the use of Bash, but in my opinion, awk is the better tool for this.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2021-06-30 22:24:10

SupKurtJ
Member
Registered: 2015-07-18
Posts: 34

Re: [SOLVED] Bash script swap colums and rows from .csv

I found a solution thanks

awk -F"," '{for(i=1;i<=NF;i++){A[NR,i]=$i};if(NF>n){n=NF}}
END{for(i=1;i<=n;i++){
for(j=1;j<=NR;j++){
s=s?s";"A[j,i]:A[j,i]}
print s;s=""}}' file

Last edited by SupKurtJ (2021-06-30 22:24:45)

Offline

#4 2021-06-30 22:53:04

bulletmark
Member
From: Brisbane, Australia
Registered: 2013-10-22
Posts: 525

Re: [SOLVED] Bash script swap colums and rows from .csv

#!/usr/bin/python
import sys
lines = [l.strip().split(';') for l in sys.stdin]
for n in range(len(lines)):
    print(';'.join(l[n] for l in lines))

Run it as `prog <file.csv`

Offline

#5 2021-06-30 23:50:13

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 29,502
Website

Re: [SOLVED] Bash script swap colums and rows from .csv

rs -c';' -C';' -T <file

https://www.freebsd.org/cgi/man.cgi?query=rs

Last edited by jasonwryan (2021-06-30 23:51:12)


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2021-07-01 00:00:42

bulletmark
Member
From: Brisbane, Australia
Registered: 2013-10-22
Posts: 525

Re: [SOLVED] Bash script swap colums and rows from .csv

@jasonwryan, so install BSD first? wink

Offline

#7 2021-07-01 00:03:34

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 17,852

Re: [SOLVED] Bash script swap colums and rows from .csv

With Jason, I always feel like the guy with a hammer, saw, and screwdriver;   He is the guy with the planner, joiner, lathe, radial arm saw, and 15 varieties of router.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#8 2021-07-01 00:20:29

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 29,502
Website

Re: [SOLVED] Bash script swap colums and rows from .csv

bulletmark wrote:

@jasonwryan, so install BSD first? wink

For a one-liner? Of course! smile

ewaller wrote:

He is the guy with the planner, joiner, lathe, radial arm saw, and 15 varieties of router.

We're all just waiting for Trilby to show up with a diesel powered chainsaw... tongue


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#9 2021-07-01 20:43:36

thiagowfx
Member
Registered: 2013-07-09
Posts: 570

Re: [SOLVED] Bash script swap colums and rows from .csv

Just to add more classic unix tools to the pile, the following seems to work as expected...

cat input | cut -f1-3 -d';' | tee >(awk -F ';' '{print $3}' | paste -sd ';') >(awk -F ';' '{print $2}' | paste -sd ';') >(awk -F ';' '{print $1}' | paste -sd ';')

Offline

Board footer

Powered by FluxBB