LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 01:06 PM   #1
udiubu
Member
 
Registered: Oct 2011
Posts: 73

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
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-27-2011, 02:56 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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, 03:15 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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, 05:58 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,003

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
sed alternative

Hi,

you can also do this with sed:
Code:
sed -r 's/(([^[:blank:]]+[[:blank:]]+){3})/\1\n/g' filename
 
1 members found this post helpful.
Old 10-27-2011, 11:20 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,816

Rep: Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071Reputation: 3071
Or maybe:
Code:
awk 'ORS=(!(NR%3)?"\n":" ")' RS=" " file
 
2 members found this post helpful.
Old 11-23-2017, 02:38 PM   #6
vizier
LQ Newbie
 
Registered: Nov 2008
Posts: 1

Rep: Reputation: 0
Deleted

Last edited by vizier; 11-23-2017 at 02:47 PM.
 
Old 11-23-2017, 08:51 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,814

Rep: Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639Reputation: 639
Another method ...

With this InFile ...
Code:
2011 2034 49558 45 39445 2345 34 3948 32
... this code ...
Code:
tr " " "\n" <$InFile |paste -s -d"  \n" >$OutFile
... produced this OutFile ...
Code:
2011 2034 49558
45 39445 2345
34 3948 32
For convenience, my InFile contains blank-delimited strings. If your InFile is tab-delimited then change the tr command to translate tabs (instead of blanks) to newlines.

Daniel B. Martin
 
Old 11-26-2017, 09:41 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,848

Rep: Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012
Assuming the file is reasonably-sized, you could do it entirely in bash, or any similar shell that has printf available:

Code:
printf "%d %d %d\n" $( <file.txt )
It relies on the shell's built in word-splitting. The unquoted $(...) expansion gets chopped up into individual words (based by default on whitespace, configurable with the IFS variable).

printf then formats the output three "%d" digits at a time. (Use "%f" for floating point numbers or "%s" for plain text strings)

$( <file.txt ) is a bash shortcut that does the same job as $( cat file.txt ).


You could also read the contents into an array first:

Code:
read -d '' -a nums <file.txt
printf '%d %d %d\n' "${nums[@]}"
This would be a bit safer, as the read builtin gives you more control over how the file gets read. The -d '' option above forces it to read the entire file at once, although your IFS setting is still used for word-splitting.

Again, I wouldn't recommend this for very large files though, as the whole thing has to get pulled into the shell's memory at once.
 
  


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 09:06 PM
[SOLVED] Transpose multiple rows into a single column wonjusup Linux - Newbie 13 04-09-2011 06:53 AM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07: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 03:09 PM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 02:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:15 AM.

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