Try with this one:
sed -e :a -e '/$/N; s/\n/ /; ta' test.txt
-e : It allows to write a sed program in several parts. For example, a
sed program with two substitution rules could be written as
sed -e 's/one/two/' -e 's/three/four' instead of
sed 's/one/two/;s/three/four'. It makes it more readable.
In this one-liner the first
-e creates a label called
: command followed by a name crates a named label.
-e uses a new command
t command branches to a named label if the last substitute command modified pattern space. This branching technique can be used to create loops in sed.
see this sample of
This is the simplest
sed one-liner possible. It uses the substitute command and applies it once on each line. It substitutes string "foo" with "bar". that I used in (
... ; s/\n/ / ; ...) at first line, it means replace
' ' whitespace)
then see this sample of
sed '$!N;s/\n/ /'
It's similar to
sed '/$/N;s/\n/ /' that I used in (
/$/N; s/\n/ / ; ...) at first line, it means join every pair of lines with a whitespace.
This one-liner joins two consecutive lines with the
N command. They get joined with a
\n character between them. The substitute command replaces this newline with a space, thus joining every pair of lines with a whitespace.
Here is the output of running this command in my test.txt file:
User@test-pc:~$ sed '/$/N;s/\n/ /' test.txt
23200 GRIFFITH LABORATORIES LTD
GRIFFITH LABORATORIES SOUTH DUBLIN COUNTY COUNCIL
OFFICE OFFICE (INDUSTRIAL)
List Rateable 2 Pineview Industrial Estate
Firhouse Road Knocklyon
31 Dec 2007 01 Jan 2008"
Alright, final command become this
sed -e :a -e '/$/N; s/\n/ /; ta' test.txt:
The first expression
:a creates a named label
a. The second expression looks to see if the current line ends with a
\n. If it does, it joins it with the line following it using the
N command. Then the
\n and the newline between joined lines get erased with
s/\n/ / command. If the substitution was successful we branch to the beginning of expression and do the same again, in hope that we might have another
\n. If the substitution was not successful, the line did not end with a
\n and we print it out.
Here is the the output or running this one-liner:
User@test-pc:~$ sed -e :a -e '/$/N; s/\n/ /; ta' test.txt
500212 262578-4-4 23200 GRIFFITH LABORATORIES LTD GRIFFITH LABORATORIES SOUTH DUBLIN COUNTY COUNCIL OFFICE OFFICE (INDUSTRIAL) List Rateable 2 Pineview Industrial Estate Firhouse Road Knocklyon 31 Dec 2007 01 Jan 2008"