LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 01-31-2007, 12:41 AM   #1
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Rep: Reputation: 30
join every three lines of a text file


How to join every three lines of a text file?
Any programming language is ok.
Thanks.
e.g.
Input:
Sep. 30, 2006 Oct. 04, 2006
PIZZA PIZZA #222
$8.75
Oct. 01, 2006 Oct. 04, 2006
BURGER KING #8818
$9.10
Oct. 02, 2006 Oct. 03, 2006
NEW GREAT WALL RESTAURANT
$38.10

Output:
Sep. 30, 2006 Oct. 04, 2006 PIZZA PIZZA #222 $8.75
Oct. 01, 2006 Oct. 04, 2006 BURGER KING #8818 $9.10
Oct. 02, 2006 Oct. 03, 2006 NEW GREAT WALL RESTAURANT $38.10
 
Old 01-31-2007, 02:17 AM   #2
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
This can be done using the unix text utility 'paste'

Use the option -d with a the list of delimiters space, space newline
And the -s option.

Imagine that the file /tmp/foo contains your input:

Sep. 30, 2006 Oct. 04, 2006
PIZZA PIZZA #222
$8.75
Oct. 01, 2006 Oct. 04, 2006
BURGER KING #8818
$9.10
Oct. 02, 2006 Oct. 03, 2006
NEW GREAT WALL RESTAURANT
$38.10

eg.

Quote:
>paste -s -d '
' /tmp/foo
Yields

Sep. 30, 2006 Oct. 04, 2006 PIZZA PIZZA #222 $8.75
Oct. 01, 2006 Oct. 04, 2006 BURGER KING #8818 $9.10
Oct. 02, 2006 Oct. 03, 2006 NEW GREAT WALL RESTAURANT $38.10

The thing that's hard to convey via text is that the delimiters are two space characters and a newline, surrounded by single quotes.
 
Old 01-31-2007, 06:47 AM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

Good solution. Some versions of paste recognize \n for newline, and \s for space:
Code:
paste -s -d "\s\s\n" /tmp/foo
which can make the command slightly easier to read.

( <rant on poor documentation> Which is not noted in the man or info pages on paste, nor provided as an example </rant> ) ... cheers, makyo
 
Old 02-01-2007, 08:23 AM   #4
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
My version of paste (version 5.2.1 with Gnu Coretutils, the default version which ships with Fedora Core 4).

Quote:
paste -s -d "\s\s\n" /tmp/foo
Gives

[QUOTE]
Sep. 30, 2006 Oct. 04, 2006sPIZZA PIZZA #222s$8.75
Oct. 01, 2006 Oct. 04, 2006sBURGER KING #8818s$9.10
Oct. 02, 2006 Oct. 03, 2006sNEW GREAT WALL RESTAURANTs$38.10
[QUOTE]

In other words the \s delimter is parsed as a literal 's' (yeah, I'm anal retentive, I looked at it in od)

Quote:
paste -s -d " \n" /tmp/foo
Does the right thing.

I agree with the documentation. This is part of Gnu coreutils, the full documentation can be found by typing 'info paste'... sort of. You have to root around in the info pages for coreutils and find the section that has character classes and special characters in it. I found it once
 
Old 02-01-2007, 09:42 AM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi, bartonski.

Good catch, thanks for checking it. The version of paste that I use is the same. Apparently reading unfamiliar text allowed me to visually skip the 's' insertions. I should have pasted the results.

I looked over info paste, and it's not different from man paste (of course, my skimming is somewhat suspect, at least recently ). I see that info coreutils paste does present a few examples, but does not appear to discuss \n.

Upon reflection, I am not sure why I even tried the \s, since that doesn't make any sense. The only use that I can think of now is for matching a whitespace character. Loose connection someplace ... cheers, makyo
 
Old 02-01-2007, 12:34 PM   #6
whk
Member
 
Registered: Jun 2005
Posts: 200

Rep: Reputation: 35
Quote:
I can think of now is for matching a whitespace character
paste -s -d " \n" /tmp/foo
(with two white spaces between " and \n)
This website should have a limited tab options.
just mho
 
Old 02-01-2007, 06:00 PM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi, whk.

Instead of using plain text in forum threads:

paste -s -d " \n" /tmp/foo

or QUOTE brackets:
Quote:
paste -s -d " \n" /tmp/foo
I think if one uses CODE brackets, one can see the spaces better because that gets set as monospace:
Code:
paste -s -d "  \n" /tmp/foo
How does that look to you? ... cheers, makyo

( edit 1: clarification )

Last edited by makyo; 02-01-2007 at 06:07 PM.
 
Old 02-01-2007, 07:42 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
If you have Python, here's an alternative
Code:
#!/usr/bin/python
data = open("file.txt").readlines()
data = [ i.strip() for i in data ] #get rid of newlines
threelines = range(0,len(data),3) 
for num,line in enumerate(data):
 	if num in threelines:
 		print ' '.join(data[num:num+3])
output:
Code:
#/home: ./test.py
Sep. 30, 2006 Oct. 04, 2006 PIZZA PIZZA #222$8.75
Oct. 01, 2006 Oct. 04, 2006 BURGER KING #8818$9.10
Oct. 02, 2006 Oct. 03, 2006 NEW GREAT WALL RESTAURANT$38.10
 
Old 02-01-2007, 11:40 PM   #9
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Quote:
Originally Posted by makyo
Upon reflection, I am not sure why I even tried the \s, since that doesn't make any sense. The only use that I can think of now is for matching a whitespace character. Loose connection someplace ... cheers, makyo
Hmm. Makes me wonder, what \S would give you. No space delimiter?
 
  


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
Join lines in text file vidyashankara Linux - General 10 12-21-2009 03:17 PM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM
adding text to lines in a file tpreitano Linux - General 2 10-04-2005 09:30 AM
How to filter lines from a text file Rascale Linux - Software 1 10-28-2003 12:46 PM
Loop over lines in text file? amaze Linux - General 2 08-12-2003 07:15 AM


All times are GMT -5. The time now is 10:55 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