Bash: split single column of text into multiple columns
Hello,
I got a text file with a single column of numbers and separators (','): Code:
1 Code:
1,224332026,0 Thank in advance. -- rob |
Read each set into an array until you find the separator, then start next array. When all done, print the equivalent entry from all arrays.
Try it, when you have problems ask for assistance. |
You can import that using spreadsheet program as data delimited by newlines versus spaces or commas and then export it back out as CSV.
|
Is this what you are asking?
Code:
awk 'BEGIN {FS="\n"; RS=",\n";}{ORS="\n"; print $1,$2,$3,$4,$5,$6,$7,$8;}' file.txt | awk '{for(c = 1; c <= NF; c++) {a[c, NR] = $c}if(max_nf < NF) {max_nf = NF}}END {for(r = 1; r <= max_nf; r++) {for(c = 1; c <= NR; c++) { printf("%s ", a[r, c])}print ""}}' |
Wow! Seems a little complicated to me :) I'll have to study it well.
It doesn't seem to work, what I got is this: Quote:
Thanks btw. |
There is issue with linuxquestions.org text editor, you can not write percent s, percent disappears automatically.
Only Advanced editor writes percent symbol. |
You're right!
Your script works as expected with a file with 8 rows/records: Quote:
thank you. |
Very bad way to use pipes with awk but..... I hope Awk creators will not see this.
Try this for different number of columns and rows. Code:
awk '{for(c = 1; c <= NF; c++) {a[c, NR] = $c}if(max_nf < NF) {max_nf = NF}}END {for(r = 1; r <= max_nf; r++) {for(c = 1; c <= NR; c++) { printf("%s ",a[r,c])}print ""}}' file.txt | awk 'BEGIN {RS=","; FS=" ";}{print $0;}' | awk '{for(c = 1; c <= NF; c++) {a[c, NR] = $c}if(max_nf < NF) {max_nf = NF}}END {for(r = 1; r <= max_nf; r++) {for(c = 1; c <= NR; c++) { printf("%s ", a[r, c])}print ""}}' |
i would find out the line number where the first ^,$ is located (grep -n).
then use split -l to create three different files. then combine them with paste -d ,. |
All times are GMT -5. The time now is 11:22 AM. |