LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-27-2011, 02:06 PM   #1
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Rep: Reputation: Disabled
Split single line into multiple lines with 3 column each


Dear all,

I have a long long string file - say file.txt:
2011 2034 49558 45 39445 2345 34 3948 32 ...

I simply want to have this single string splitted into multiple lines, each of which contains exactly three of the numbers above. See example below:

2011 2034 49558
45 39445 2345
34 3948 32

The numbers in file.txt are Tabs-spaced, but I can easily substitute the spaces with commas if that helps. Also, I cannot use fold command as the number of characters across the digits obviously vary (i.e. see 49558 vs 34).

Suggestions are highly appreciated.

Best,

Udiubu
 
Old 10-27-2011, 03:56 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
How about replacing tabs with newlines first, and then merging 3 lines at a time? In bash, it could be something like this, considering the list is tab separated:

Code:
cat "file.txt" | tr "\t" "\n" | while read N1; do read N2; read N3; echo "$N1 $N2 $N3"; done
You may have to modify this to fit your needs.
Of course, there are many other solutions, most of them probably better than this one. It could be done in just about any language.
 
Old 10-27-2011, 04:15 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Or a simple loop in awk:
Code:
awk '{for (i = 1; i <= NF; i += 3) printf "%d %d %d\n", $i, $(i+1), $(i+2)}' file
 
Old 10-27-2011, 06:58 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
sed alternative

Hi,

you can also do this with sed:
Code:
sed -r 's/(([^[:blank:]]+[[:blank:]]+){3})/\1\n/g' filename
 
Old 10-28-2011, 12:20 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,627

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Or maybe:
Code:
awk 'ORS=(!(NR%3)?"\n":" ")' RS=" " file
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How to split single column string into two columns Colorinb Linux - Newbie 2 10-07-2011 10:06 PM
[SOLVED] Transpose multiple rows into a single column wonjusup Linux - Newbie 13 04-09-2011 07:53 AM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 08:23 PM
how to create a single line of output from multiple variable lines of input steven.c.banks Linux - General 2 02-03-2010 04:09 PM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 03:38 AM


All times are GMT -5. The time now is 05:36 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration