simple script rotating table
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
add a comment |
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
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
add a comment |
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
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
bash sed awk
edited 3 hours ago
Rui F Ribeiro
41.5k1483140
41.5k1483140
asked Mar 26 '12 at 15:08
rushrush
19.3k46595
19.3k46595
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
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 abash
way.
– rozcietrzewiacz
Aug 24 '12 at 15:41
add a comment |
flds=3; for((i=1;i<=flds;i++));do
printf '%s' "$(cut -d';' -f$i file)" |tr 'n' ';';echo
done
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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 abash
way.
– rozcietrzewiacz
Aug 24 '12 at 15:41
add a comment |
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
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 abash
way.
– rozcietrzewiacz
Aug 24 '12 at 15:41
add a comment |
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
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
edited May 23 '17 at 12:39
Community♦
1
1
answered Mar 26 '12 at 15:57
rozcietrzewiaczrozcietrzewiacz
29.4k47392
29.4k47392
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 abash
way.
– rozcietrzewiacz
Aug 24 '12 at 15:41
add a comment |
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 abash
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
add a comment |
flds=3; for((i=1;i<=flds;i++));do
printf '%s' "$(cut -d';' -f$i file)" |tr 'n' ';';echo
done
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
add a comment |
flds=3; for((i=1;i<=flds;i++));do
printf '%s' "$(cut -d';' -f$i file)" |tr 'n' ';';echo
done
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
add a comment |
flds=3; for((i=1;i<=flds;i++));do
printf '%s' "$(cut -d';' -f$i file)" |tr 'n' ';';echo
done
flds=3; for((i=1;i<=flds;i++));do
printf '%s' "$(cut -d';' -f$i file)" |tr 'n' ';';echo
done
answered Mar 26 '12 at 17:50
Peter.OPeter.O
19.1k1891146
19.1k1891146
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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