Transpose a single column of a CSV file
up vote
0
down vote
favorite
I have a CSV file with two columns namely
column 1: file name
column 2: access status
The following are some sample recotds
FileA, CREATE
FileA, MODIFY
FileA, DELETE
FileB, CREATE
FileB, MODIFY
I need to TRANSPOSE values of the second column into a single row based on the distinct values of the first column.
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
shell-script shell
add a comment |
up vote
0
down vote
favorite
I have a CSV file with two columns namely
column 1: file name
column 2: access status
The following are some sample recotds
FileA, CREATE
FileA, MODIFY
FileA, DELETE
FileB, CREATE
FileB, MODIFY
I need to TRANSPOSE values of the second column into a single row based on the distinct values of the first column.
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
shell-script shell
Do you need to keep the order? I mean, isFileA, CREATE|MODIFY|DELETEthe same asFileA, DELETE|CREATE|MODIFY?
– terdon♦
2 days ago
stackoverflow.com/questions/32940758/…
– Emilio Galarraga
2 days ago
No, ordering based on second column is required
– Kumarjit Ghosh
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a CSV file with two columns namely
column 1: file name
column 2: access status
The following are some sample recotds
FileA, CREATE
FileA, MODIFY
FileA, DELETE
FileB, CREATE
FileB, MODIFY
I need to TRANSPOSE values of the second column into a single row based on the distinct values of the first column.
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
shell-script shell
I have a CSV file with two columns namely
column 1: file name
column 2: access status
The following are some sample recotds
FileA, CREATE
FileA, MODIFY
FileA, DELETE
FileB, CREATE
FileB, MODIFY
I need to TRANSPOSE values of the second column into a single row based on the distinct values of the first column.
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
shell-script shell
shell-script shell
edited 2 days ago
Rui F Ribeiro
38.5k1479128
38.5k1479128
asked 2 days ago
Kumarjit Ghosh
102
102
Do you need to keep the order? I mean, isFileA, CREATE|MODIFY|DELETEthe same asFileA, DELETE|CREATE|MODIFY?
– terdon♦
2 days ago
stackoverflow.com/questions/32940758/…
– Emilio Galarraga
2 days ago
No, ordering based on second column is required
– Kumarjit Ghosh
yesterday
add a comment |
Do you need to keep the order? I mean, isFileA, CREATE|MODIFY|DELETEthe same asFileA, DELETE|CREATE|MODIFY?
– terdon♦
2 days ago
stackoverflow.com/questions/32940758/…
– Emilio Galarraga
2 days ago
No, ordering based on second column is required
– Kumarjit Ghosh
yesterday
Do you need to keep the order? I mean, is
FileA, CREATE|MODIFY|DELETE the same as FileA, DELETE|CREATE|MODIFY?– terdon♦
2 days ago
Do you need to keep the order? I mean, is
FileA, CREATE|MODIFY|DELETE the same as FileA, DELETE|CREATE|MODIFY?– terdon♦
2 days ago
stackoverflow.com/questions/32940758/…
– Emilio Galarraga
2 days ago
stackoverflow.com/questions/32940758/…
– Emilio Galarraga
2 days ago
No, ordering based on second column is required
– Kumarjit Ghosh
yesterday
No, ordering based on second column is required
– Kumarjit Ghosh
yesterday
add a comment |
5 Answers
5
active
oldest
votes
up vote
1
down vote
If you don't care about the order the commands are in, you can use:
$ awk -F"[, ]" '{
a[$1][$2]++
}
END{
for(i in a){
printf "%s,",i;
for(k in a[i]){
printf "%s|", k
}
print ""
}
}' file | sed 's/|$//'
FileA, DELETE|CREATE|MODIFY
FileB, CREATE|MODIFY
If you need the order, you can apply some perl magic:
$ sed 's/ //' file |
perl -F, -lne 'push @{$k{$F[0]}},$F[1]; }{
print "$_, ",join "|", @{$k{$_}} for keys(%k);'
FileB, CREATE|MODIFY
FileA, CREATE|MODIFY|DELETE
add a comment |
up vote
1
down vote
awk '1 {if (a[$1]) {a[$1] = a[$1]" "$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,a[i]}}' file |sed 's/.$//'
add a comment |
up vote
1
down vote
Try also
awk '
$1 != LAST {printf "%s%s ", LD, $1 # print every new COL1 value
LAST = $1 # and remeber it
LD = RS # set the line delimiter (empty at program start)
FD = "" # unset field delimiter
}
{printf "%s%s", FD, $2 # print successive second fields, after field delim
FD = "|" # set the field delimiter
}
END {printf RS # last action: new line
}
' file
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
add a comment |
up vote
1
down vote
And a fourth one :D
awk '1 {if (a[$1]) {a[$1] = a[$1]$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,gensub( /|$/,"","1",a[i])}}' kumarjit
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
What does that first1do? I'd expect it to print the unmodified line.
– terdon♦
2 days ago
add a comment |
up vote
1
down vote
To output in sorted order, with GNU awk
gawk -F', ' '
{ a[$1] = a[$1] "|" $2 }
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
for (b in a) print b ", " substr(a[b], 2)
}
'
To output in the original order of the keys:
awk -F', ' '
!($1 in a) { keys[++count] = $1 }
{ a[$1] = a[$1] "|" $2 }
END {
for (i = 1; i <= count; i++)
print keys[i] ", " substr(a[keys[i]], 2)
}
'
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
If you don't care about the order the commands are in, you can use:
$ awk -F"[, ]" '{
a[$1][$2]++
}
END{
for(i in a){
printf "%s,",i;
for(k in a[i]){
printf "%s|", k
}
print ""
}
}' file | sed 's/|$//'
FileA, DELETE|CREATE|MODIFY
FileB, CREATE|MODIFY
If you need the order, you can apply some perl magic:
$ sed 's/ //' file |
perl -F, -lne 'push @{$k{$F[0]}},$F[1]; }{
print "$_, ",join "|", @{$k{$_}} for keys(%k);'
FileB, CREATE|MODIFY
FileA, CREATE|MODIFY|DELETE
add a comment |
up vote
1
down vote
If you don't care about the order the commands are in, you can use:
$ awk -F"[, ]" '{
a[$1][$2]++
}
END{
for(i in a){
printf "%s,",i;
for(k in a[i]){
printf "%s|", k
}
print ""
}
}' file | sed 's/|$//'
FileA, DELETE|CREATE|MODIFY
FileB, CREATE|MODIFY
If you need the order, you can apply some perl magic:
$ sed 's/ //' file |
perl -F, -lne 'push @{$k{$F[0]}},$F[1]; }{
print "$_, ",join "|", @{$k{$_}} for keys(%k);'
FileB, CREATE|MODIFY
FileA, CREATE|MODIFY|DELETE
add a comment |
up vote
1
down vote
up vote
1
down vote
If you don't care about the order the commands are in, you can use:
$ awk -F"[, ]" '{
a[$1][$2]++
}
END{
for(i in a){
printf "%s,",i;
for(k in a[i]){
printf "%s|", k
}
print ""
}
}' file | sed 's/|$//'
FileA, DELETE|CREATE|MODIFY
FileB, CREATE|MODIFY
If you need the order, you can apply some perl magic:
$ sed 's/ //' file |
perl -F, -lne 'push @{$k{$F[0]}},$F[1]; }{
print "$_, ",join "|", @{$k{$_}} for keys(%k);'
FileB, CREATE|MODIFY
FileA, CREATE|MODIFY|DELETE
If you don't care about the order the commands are in, you can use:
$ awk -F"[, ]" '{
a[$1][$2]++
}
END{
for(i in a){
printf "%s,",i;
for(k in a[i]){
printf "%s|", k
}
print ""
}
}' file | sed 's/|$//'
FileA, DELETE|CREATE|MODIFY
FileB, CREATE|MODIFY
If you need the order, you can apply some perl magic:
$ sed 's/ //' file |
perl -F, -lne 'push @{$k{$F[0]}},$F[1]; }{
print "$_, ",join "|", @{$k{$_}} for keys(%k);'
FileB, CREATE|MODIFY
FileA, CREATE|MODIFY|DELETE
answered 2 days ago
terdon♦
127k31245422
127k31245422
add a comment |
add a comment |
up vote
1
down vote
awk '1 {if (a[$1]) {a[$1] = a[$1]" "$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,a[i]}}' file |sed 's/.$//'
add a comment |
up vote
1
down vote
awk '1 {if (a[$1]) {a[$1] = a[$1]" "$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,a[i]}}' file |sed 's/.$//'
add a comment |
up vote
1
down vote
up vote
1
down vote
awk '1 {if (a[$1]) {a[$1] = a[$1]" "$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,a[i]}}' file |sed 's/.$//'
awk '1 {if (a[$1]) {a[$1] = a[$1]" "$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,a[i]}}' file |sed 's/.$//'
answered 2 days ago
Emilio Galarraga
34829
34829
add a comment |
add a comment |
up vote
1
down vote
Try also
awk '
$1 != LAST {printf "%s%s ", LD, $1 # print every new COL1 value
LAST = $1 # and remeber it
LD = RS # set the line delimiter (empty at program start)
FD = "" # unset field delimiter
}
{printf "%s%s", FD, $2 # print successive second fields, after field delim
FD = "|" # set the field delimiter
}
END {printf RS # last action: new line
}
' file
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
add a comment |
up vote
1
down vote
Try also
awk '
$1 != LAST {printf "%s%s ", LD, $1 # print every new COL1 value
LAST = $1 # and remeber it
LD = RS # set the line delimiter (empty at program start)
FD = "" # unset field delimiter
}
{printf "%s%s", FD, $2 # print successive second fields, after field delim
FD = "|" # set the field delimiter
}
END {printf RS # last action: new line
}
' file
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
add a comment |
up vote
1
down vote
up vote
1
down vote
Try also
awk '
$1 != LAST {printf "%s%s ", LD, $1 # print every new COL1 value
LAST = $1 # and remeber it
LD = RS # set the line delimiter (empty at program start)
FD = "" # unset field delimiter
}
{printf "%s%s", FD, $2 # print successive second fields, after field delim
FD = "|" # set the field delimiter
}
END {printf RS # last action: new line
}
' file
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
Try also
awk '
$1 != LAST {printf "%s%s ", LD, $1 # print every new COL1 value
LAST = $1 # and remeber it
LD = RS # set the line delimiter (empty at program start)
FD = "" # unset field delimiter
}
{printf "%s%s", FD, $2 # print successive second fields, after field delim
FD = "|" # set the field delimiter
}
END {printf RS # last action: new line
}
' file
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
answered 2 days ago
RudiC
3,8381312
3,8381312
add a comment |
add a comment |
up vote
1
down vote
And a fourth one :D
awk '1 {if (a[$1]) {a[$1] = a[$1]$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,gensub( /|$/,"","1",a[i])}}' kumarjit
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
What does that first1do? I'd expect it to print the unmodified line.
– terdon♦
2 days ago
add a comment |
up vote
1
down vote
And a fourth one :D
awk '1 {if (a[$1]) {a[$1] = a[$1]$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,gensub( /|$/,"","1",a[i])}}' kumarjit
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
What does that first1do? I'd expect it to print the unmodified line.
– terdon♦
2 days ago
add a comment |
up vote
1
down vote
up vote
1
down vote
And a fourth one :D
awk '1 {if (a[$1]) {a[$1] = a[$1]$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,gensub( /|$/,"","1",a[i])}}' kumarjit
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
And a fourth one :D
awk '1 {if (a[$1]) {a[$1] = a[$1]$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,gensub( /|$/,"","1",a[i])}}' kumarjit
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY
answered 2 days ago
tink
4,08411218
4,08411218
What does that first1do? I'd expect it to print the unmodified line.
– terdon♦
2 days ago
add a comment |
What does that first1do? I'd expect it to print the unmodified line.
– terdon♦
2 days ago
What does that first
1 do? I'd expect it to print the unmodified line.– terdon♦
2 days ago
What does that first
1 do? I'd expect it to print the unmodified line.– terdon♦
2 days ago
add a comment |
up vote
1
down vote
To output in sorted order, with GNU awk
gawk -F', ' '
{ a[$1] = a[$1] "|" $2 }
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
for (b in a) print b ", " substr(a[b], 2)
}
'
To output in the original order of the keys:
awk -F', ' '
!($1 in a) { keys[++count] = $1 }
{ a[$1] = a[$1] "|" $2 }
END {
for (i = 1; i <= count; i++)
print keys[i] ", " substr(a[keys[i]], 2)
}
'
add a comment |
up vote
1
down vote
To output in sorted order, with GNU awk
gawk -F', ' '
{ a[$1] = a[$1] "|" $2 }
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
for (b in a) print b ", " substr(a[b], 2)
}
'
To output in the original order of the keys:
awk -F', ' '
!($1 in a) { keys[++count] = $1 }
{ a[$1] = a[$1] "|" $2 }
END {
for (i = 1; i <= count; i++)
print keys[i] ", " substr(a[keys[i]], 2)
}
'
add a comment |
up vote
1
down vote
up vote
1
down vote
To output in sorted order, with GNU awk
gawk -F', ' '
{ a[$1] = a[$1] "|" $2 }
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
for (b in a) print b ", " substr(a[b], 2)
}
'
To output in the original order of the keys:
awk -F', ' '
!($1 in a) { keys[++count] = $1 }
{ a[$1] = a[$1] "|" $2 }
END {
for (i = 1; i <= count; i++)
print keys[i] ", " substr(a[keys[i]], 2)
}
'
To output in sorted order, with GNU awk
gawk -F', ' '
{ a[$1] = a[$1] "|" $2 }
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
for (b in a) print b ", " substr(a[b], 2)
}
'
To output in the original order of the keys:
awk -F', ' '
!($1 in a) { keys[++count] = $1 }
{ a[$1] = a[$1] "|" $2 }
END {
for (i = 1; i <= count; i++)
print keys[i] ", " substr(a[keys[i]], 2)
}
'
answered 2 days ago
glenn jackman
50k569106
50k569106
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f487150%2ftranspose-a-single-column-of-a-csv-file%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
Do you need to keep the order? I mean, is
FileA, CREATE|MODIFY|DELETEthe same asFileA, DELETE|CREATE|MODIFY?– terdon♦
2 days ago
stackoverflow.com/questions/32940758/…
– Emilio Galarraga
2 days ago
No, ordering based on second column is required
– Kumarjit Ghosh
yesterday