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 |
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:
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. |
Hi.
Good solution. Some versions of paste recognize \n for newline, and \s for space: Code:
paste -s -d "\s\s\n" /tmp/foo ( <rant on poor documentation> Which is not noted in the man or info pages on paste, nor provided as an example </rant> ) ... cheers, makyo |
My version of paste (version 5.2.1 with Gnu Coretutils, the default version which ships with Fedora Core 4).
Quote:
[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:
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 ;) |
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 |
Quote:
(with two white spaces between " and \n) This website should have a limited tab options. just mho |
Hi, whk.
Instead of using plain text in forum threads: paste -s -d " \n" /tmp/foo or QUOTE brackets: Quote:
Code:
paste -s -d " \n" /tmp/foo ( edit 1: clarification ) |
If you have Python, here's an alternative
Code:
#!/usr/bin/python Code:
#/home: ./test.py |
Quote:
|
All times are GMT -5. The time now is 03:04 PM. |