simple script rotating table

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I'm trying to write a script, that just rotates csv table.
I mean I have some file:



head1;head2;head3
field11;field12;field13


and so on.
All I want, just to make my file



head1;field11;field21
head2;fielad12;field22
head3;field13;field23


I even haven't any idea about how it may work.
I don't ask you to write a script, I need idea about how I can maje it in standart shell (unfortunately I'm not able to use bashism or gnu extensions, POSIX only)



ps. Actually I can do it, but only in ugly way with unbelievable enefficient multirereading file with table. I believe there is more beautiful way.










share|improve this question



















  • 2





    Duplicate of stackoverflow.com/questions/1729824/transpose-a-file-in-bash ?

    – dying_sphynx
    Mar 26 '12 at 15:19











  • @dying_sphynx Almost... This is a more general case, because the separators make some difference. I gave an example of bash adaptation for semicolons below and let others decide whether the difference is enough to call this question unique.

    – rozcietrzewiacz
    Mar 26 '12 at 16:16












  • Actually I just couldn't find this question on SO. It's pretty similar and seems to me the difference in separator isn't enough to make this question unique. Thank you for link on original question.

    – rush
    Mar 26 '12 at 16:35


















3















I'm trying to write a script, that just rotates csv table.
I mean I have some file:



head1;head2;head3
field11;field12;field13


and so on.
All I want, just to make my file



head1;field11;field21
head2;fielad12;field22
head3;field13;field23


I even haven't any idea about how it may work.
I don't ask you to write a script, I need idea about how I can maje it in standart shell (unfortunately I'm not able to use bashism or gnu extensions, POSIX only)



ps. Actually I can do it, but only in ugly way with unbelievable enefficient multirereading file with table. I believe there is more beautiful way.










share|improve this question



















  • 2





    Duplicate of stackoverflow.com/questions/1729824/transpose-a-file-in-bash ?

    – dying_sphynx
    Mar 26 '12 at 15:19











  • @dying_sphynx Almost... This is a more general case, because the separators make some difference. I gave an example of bash adaptation for semicolons below and let others decide whether the difference is enough to call this question unique.

    – rozcietrzewiacz
    Mar 26 '12 at 16:16












  • Actually I just couldn't find this question on SO. It's pretty similar and seems to me the difference in separator isn't enough to make this question unique. Thank you for link on original question.

    – rush
    Mar 26 '12 at 16:35














3












3








3








I'm trying to write a script, that just rotates csv table.
I mean I have some file:



head1;head2;head3
field11;field12;field13


and so on.
All I want, just to make my file



head1;field11;field21
head2;fielad12;field22
head3;field13;field23


I even haven't any idea about how it may work.
I don't ask you to write a script, I need idea about how I can maje it in standart shell (unfortunately I'm not able to use bashism or gnu extensions, POSIX only)



ps. Actually I can do it, but only in ugly way with unbelievable enefficient multirereading file with table. I believe there is more beautiful way.










share|improve this question
















I'm trying to write a script, that just rotates csv table.
I mean I have some file:



head1;head2;head3
field11;field12;field13


and so on.
All I want, just to make my file



head1;field11;field21
head2;fielad12;field22
head3;field13;field23


I even haven't any idea about how it may work.
I don't ask you to write a script, I need idea about how I can maje it in standart shell (unfortunately I'm not able to use bashism or gnu extensions, POSIX only)



ps. Actually I can do it, but only in ugly way with unbelievable enefficient multirereading file with table. I believe there is more beautiful way.







bash sed awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 18 at 2:03









Rui F Ribeiro

42.1k1484142




42.1k1484142










asked Mar 26 '12 at 15:08









rushrush

19.6k46696




19.6k46696







  • 2





    Duplicate of stackoverflow.com/questions/1729824/transpose-a-file-in-bash ?

    – dying_sphynx
    Mar 26 '12 at 15:19











  • @dying_sphynx Almost... This is a more general case, because the separators make some difference. I gave an example of bash adaptation for semicolons below and let others decide whether the difference is enough to call this question unique.

    – rozcietrzewiacz
    Mar 26 '12 at 16:16












  • Actually I just couldn't find this question on SO. It's pretty similar and seems to me the difference in separator isn't enough to make this question unique. Thank you for link on original question.

    – rush
    Mar 26 '12 at 16:35













  • 2





    Duplicate of stackoverflow.com/questions/1729824/transpose-a-file-in-bash ?

    – dying_sphynx
    Mar 26 '12 at 15:19











  • @dying_sphynx Almost... This is a more general case, because the separators make some difference. I gave an example of bash adaptation for semicolons below and let others decide whether the difference is enough to call this question unique.

    – rozcietrzewiacz
    Mar 26 '12 at 16:16












  • Actually I just couldn't find this question on SO. It's pretty similar and seems to me the difference in separator isn't enough to make this question unique. Thank you for link on original question.

    – rush
    Mar 26 '12 at 16:35








2




2





Duplicate of stackoverflow.com/questions/1729824/transpose-a-file-in-bash ?

– dying_sphynx
Mar 26 '12 at 15:19





Duplicate of stackoverflow.com/questions/1729824/transpose-a-file-in-bash ?

– dying_sphynx
Mar 26 '12 at 15:19













@dying_sphynx Almost... This is a more general case, because the separators make some difference. I gave an example of bash adaptation for semicolons below and let others decide whether the difference is enough to call this question unique.

– rozcietrzewiacz
Mar 26 '12 at 16:16






@dying_sphynx Almost... This is a more general case, because the separators make some difference. I gave an example of bash adaptation for semicolons below and let others decide whether the difference is enough to call this question unique.

– rozcietrzewiacz
Mar 26 '12 at 16:16














Actually I just couldn't find this question on SO. It's pretty similar and seems to me the difference in separator isn't enough to make this question unique. Thank you for link on original question.

– rush
Mar 26 '12 at 16:35






Actually I just couldn't find this question on SO. It's pretty similar and seems to me the difference in separator isn't enough to make this question unique. Thank you for link on original question.

– rush
Mar 26 '12 at 16:35











2 Answers
2






active

oldest

votes


















1














Here's a quick adaptation of the bash solution to this similar SO question for the particular separators you have (semicolons):



declare -a array=( ) # we build a 1-D-array

IFS=';' read -a line < "$1" # read the headline

COLS=$#line[@] # save number of columns

index=0
while IFS=';' read -a line ; do
for (( COUNTER=0; COUNTER<$#line[@]; COUNTER++ )); do
array[$index]=$line[$COUNTER]
((index++))
done
done < "$1"

for (( ROW = 0; ROW < COLS; ROW++ )); do
printf "%s" $array[$ROW]
for (( COUNTER = ROW+COLS; COUNTER < $#array[@]; COUNTER += COLS )); do
printf ";%s" $array[$COUNTER]
done
printf "n"
done





share|improve this answer

























  • PS. Actually solution with awk that is in SO works much better. But thank you anyway.

    – rush
    Mar 26 '12 at 18:08











  • @rush Yes, awk is much faster in this kind of operations. It is definitely worth learning! I myself don't know it well enough yet, so I proposed a bash way.

    – rozcietrzewiacz
    Aug 24 '12 at 15:41



















0














flds=3; for((i=1;i<=flds;i++));do
printf '%s' "$(cut -d';' -f$i file)" |tr 'n' ';';echo
done





share|improve this answer























  • Thanks, but that's not the solution I'm looking for. In this case I need read file as many times as columns I have. It's useful only for small files. Not my case.

    – rush
    Mar 26 '12 at 18:07












Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f35062%2fsimple-script-rotating-table%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Here's a quick adaptation of the bash solution to this similar SO question for the particular separators you have (semicolons):



declare -a array=( ) # we build a 1-D-array

IFS=';' read -a line < "$1" # read the headline

COLS=$#line[@] # save number of columns

index=0
while IFS=';' read -a line ; do
for (( COUNTER=0; COUNTER<$#line[@]; COUNTER++ )); do
array[$index]=$line[$COUNTER]
((index++))
done
done < "$1"

for (( ROW = 0; ROW < COLS; ROW++ )); do
printf "%s" $array[$ROW]
for (( COUNTER = ROW+COLS; COUNTER < $#array[@]; COUNTER += COLS )); do
printf ";%s" $array[$COUNTER]
done
printf "n"
done





share|improve this answer

























  • PS. Actually solution with awk that is in SO works much better. But thank you anyway.

    – rush
    Mar 26 '12 at 18:08











  • @rush Yes, awk is much faster in this kind of operations. It is definitely worth learning! I myself don't know it well enough yet, so I proposed a bash way.

    – rozcietrzewiacz
    Aug 24 '12 at 15:41
















1














Here's a quick adaptation of the bash solution to this similar SO question for the particular separators you have (semicolons):



declare -a array=( ) # we build a 1-D-array

IFS=';' read -a line < "$1" # read the headline

COLS=$#line[@] # save number of columns

index=0
while IFS=';' read -a line ; do
for (( COUNTER=0; COUNTER<$#line[@]; COUNTER++ )); do
array[$index]=$line[$COUNTER]
((index++))
done
done < "$1"

for (( ROW = 0; ROW < COLS; ROW++ )); do
printf "%s" $array[$ROW]
for (( COUNTER = ROW+COLS; COUNTER < $#array[@]; COUNTER += COLS )); do
printf ";%s" $array[$COUNTER]
done
printf "n"
done





share|improve this answer

























  • PS. Actually solution with awk that is in SO works much better. But thank you anyway.

    – rush
    Mar 26 '12 at 18:08











  • @rush Yes, awk is much faster in this kind of operations. It is definitely worth learning! I myself don't know it well enough yet, so I proposed a bash way.

    – rozcietrzewiacz
    Aug 24 '12 at 15:41














1












1








1







Here's a quick adaptation of the bash solution to this similar SO question for the particular separators you have (semicolons):



declare -a array=( ) # we build a 1-D-array

IFS=';' read -a line < "$1" # read the headline

COLS=$#line[@] # save number of columns

index=0
while IFS=';' read -a line ; do
for (( COUNTER=0; COUNTER<$#line[@]; COUNTER++ )); do
array[$index]=$line[$COUNTER]
((index++))
done
done < "$1"

for (( ROW = 0; ROW < COLS; ROW++ )); do
printf "%s" $array[$ROW]
for (( COUNTER = ROW+COLS; COUNTER < $#array[@]; COUNTER += COLS )); do
printf ";%s" $array[$COUNTER]
done
printf "n"
done





share|improve this answer















Here's a quick adaptation of the bash solution to this similar SO question for the particular separators you have (semicolons):



declare -a array=( ) # we build a 1-D-array

IFS=';' read -a line < "$1" # read the headline

COLS=$#line[@] # save number of columns

index=0
while IFS=';' read -a line ; do
for (( COUNTER=0; COUNTER<$#line[@]; COUNTER++ )); do
array[$index]=$line[$COUNTER]
((index++))
done
done < "$1"

for (( ROW = 0; ROW < COLS; ROW++ )); do
printf "%s" $array[$ROW]
for (( COUNTER = ROW+COLS; COUNTER < $#array[@]; COUNTER += COLS )); do
printf ";%s" $array[$COUNTER]
done
printf "n"
done






share|improve this answer














share|improve this answer



share|improve this answer








edited May 23 '17 at 12:39









Community

1




1










answered Mar 26 '12 at 15:57









rozcietrzewiaczrozcietrzewiacz

29.6k47392




29.6k47392












  • PS. Actually solution with awk that is in SO works much better. But thank you anyway.

    – rush
    Mar 26 '12 at 18:08











  • @rush Yes, awk is much faster in this kind of operations. It is definitely worth learning! I myself don't know it well enough yet, so I proposed a bash way.

    – rozcietrzewiacz
    Aug 24 '12 at 15:41


















  • PS. Actually solution with awk that is in SO works much better. But thank you anyway.

    – rush
    Mar 26 '12 at 18:08











  • @rush Yes, awk is much faster in this kind of operations. It is definitely worth learning! I myself don't know it well enough yet, so I proposed a bash way.

    – rozcietrzewiacz
    Aug 24 '12 at 15:41

















PS. Actually solution with awk that is in SO works much better. But thank you anyway.

– rush
Mar 26 '12 at 18:08





PS. Actually solution with awk that is in SO works much better. But thank you anyway.

– rush
Mar 26 '12 at 18:08













@rush Yes, awk is much faster in this kind of operations. It is definitely worth learning! I myself don't know it well enough yet, so I proposed a bash way.

– rozcietrzewiacz
Aug 24 '12 at 15:41






@rush Yes, awk is much faster in this kind of operations. It is definitely worth learning! I myself don't know it well enough yet, so I proposed a bash way.

– rozcietrzewiacz
Aug 24 '12 at 15:41














0














flds=3; for((i=1;i<=flds;i++));do
printf '%s' "$(cut -d';' -f$i file)" |tr 'n' ';';echo
done





share|improve this answer























  • Thanks, but that's not the solution I'm looking for. In this case I need read file as many times as columns I have. It's useful only for small files. Not my case.

    – rush
    Mar 26 '12 at 18:07
















0














flds=3; for((i=1;i<=flds;i++));do
printf '%s' "$(cut -d';' -f$i file)" |tr 'n' ';';echo
done





share|improve this answer























  • Thanks, but that's not the solution I'm looking for. In this case I need read file as many times as columns I have. It's useful only for small files. Not my case.

    – rush
    Mar 26 '12 at 18:07














0












0








0







flds=3; for((i=1;i<=flds;i++));do
printf '%s' "$(cut -d';' -f$i file)" |tr 'n' ';';echo
done





share|improve this answer













flds=3; for((i=1;i<=flds;i++));do
printf '%s' "$(cut -d';' -f$i file)" |tr 'n' ';';echo
done






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 26 '12 at 17:50









Peter.OPeter.O

19.2k1891146




19.2k1891146












  • Thanks, but that's not the solution I'm looking for. In this case I need read file as many times as columns I have. It's useful only for small files. Not my case.

    – rush
    Mar 26 '12 at 18:07


















  • Thanks, but that's not the solution I'm looking for. In this case I need read file as many times as columns I have. It's useful only for small files. Not my case.

    – rush
    Mar 26 '12 at 18:07

















Thanks, but that's not the solution I'm looking for. In this case I need read file as many times as columns I have. It's useful only for small files. Not my case.

– rush
Mar 26 '12 at 18:07






Thanks, but that's not the solution I'm looking for. In this case I need read file as many times as columns I have. It's useful only for small files. Not my case.

– rush
Mar 26 '12 at 18:07


















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f35062%2fsimple-script-rotating-table%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay