LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-22-2015, 04:40 AM   #1
robcar
LQ Newbie
 
Registered: May 2015
Posts: 6

Rep: Reputation: Disabled
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
2
3
4
5
6
7
8
,
224332026
476719
103296846
199382801
95473897
313718457
90530066
103352501
,
0
0
0
0
30537653
106365914
0
0
I would like to split this single line into multiple columns, with the "," as separator, like this:

Code:
1,224332026,0
2,476719,0
3,103296846,0
4,199382801,0
5,95473897,30537653
6,313718457,106365914
7,90530066,0
8,103352501,0
How can I do that above?
Thank in advance.
--
rob
 
Old 07-22-2015, 05:36 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
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.

Last edited by syg00; 07-22-2015 at 05:38 AM. Reason: Removed duplicate
 
Old 07-22-2015, 08:14 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You can import that using spreadsheet program as data delimited by newlines versus spaces or commas and then export it back out as CSV.
 
Old 07-23-2015, 03:49 AM   #4
eklavya
Member
 
Registered: Mar 2013
Posts: 636

Rep: Reputation: 142Reputation: 142
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 ""}}'

Last edited by eklavya; 07-23-2015 at 04:29 AM.
 
Old 07-23-2015, 04:19 AM   #5
robcar
LQ Newbie
 
Registered: May 2015
Posts: 6

Original Poster
Rep: Reputation: Disabled
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:
s s s
s s s
s s s
s s s
s s s
s s s
s s s
s s s
More important is that I could have different files, with more than 8 'rows' (not more than 150 though - they are ethernet switch ports) and it seems that your code is not contemplating that.
Thanks btw.
 
Old 07-23-2015, 04:22 AM   #6
eklavya
Member
 
Registered: Mar 2013
Posts: 636

Rep: Reputation: 142Reputation: 142
There is issue with linuxquestions.org text editor, you can not write percent s, percent disappears automatically.
Only Advanced editor writes percent symbol.

Last edited by eklavya; 07-23-2015 at 04:29 AM.
 
Old 07-23-2015, 05:58 AM   #7
robcar
LQ Newbie
 
Registered: May 2015
Posts: 6

Original Poster
Rep: Reputation: Disabled
You're right!
Your script works as expected with a file with 8 rows/records:

Quote:
1 224332026 0
2 476719 0
3 103296846 0
4 199382801 0
5 95473897 30537653
6 313718457 106365914
7 90530066 0
8 103352501 0
Now I only have to understand how to modify it when I have files with more rows/records.
thank you.
 
Old 07-23-2015, 08:27 AM   #8
eklavya
Member
 
Registered: Mar 2013
Posts: 636

Rep: Reputation: 142Reputation: 142
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 ""}}'
Replace file.txt with your file path.
 
Old 07-23-2015, 08:46 AM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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 ,.

Last edited by schneidz; 07-23-2015 at 08:54 AM.
 
1 members found this post helpful.
  


Reply

Tags
bash, columns



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Split single line into multiple lines with 3 column each udiubu Programming 7 11-26-2017 09:41 AM
awk split single column into multiple columns based on RS wolverene13 Programming 11 11-01-2012 05:07 PM
BASH or AWK: extract columns in multiple files and combine to a single file cristalp Programming 2 03-15-2012 11:55 AM
how to merge multiple columns into one column linuxon Linux - Newbie 6 03-14-2012 11:17 AM
[SOLVED] How to split single column string into two columns Colorinb Linux - Newbie 2 10-07-2011 09:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration