2
votes

I have some text files where each file will have some information as below

235.91 245.67 B: some information here
246.79 246.99 A: some other information here,
more information here,
and may be here
247.45 248.99 A: some other text here,
some more here
249.98 ---- 

and the pattern repeats

I want the text to be arranged as follows:

235.91 245.67 B: some information here
246.79 246.99 A: some other information here, more information here, and may be here
247.45 248.99 A: some other text here. some more here
249.98 -----

That means I want to merge all the lines between two matching patterns( with spaces between them)

I want every line to start with the numbers as the pattern. The numbers always has a decimal point with two digits after the decimal point. The number of lines between a pattern and the next pattern varies (There can be one or more lines or no lines at all).

Could some one help me do this using shell scripting preferably using awk?

3

3 Answers

7
votes

It sounds like you need something like this:

awk '
{ printf "%s%s", ($1 ~ /\.[[:digit:]][[:digit:]]/ ? rs : FS), $0; rs=RS }
END { print "" }
' file
1
votes

@EdMorton's answer is niftier but this also works.

$1 ~ /^[[:digit:]]+\.[[:digit:]][[:digit:]]$/ { if (NR-1) {print ""} printf "%s", $0; next }
{printf " %s", $0}
END { print ""}
1
votes

I don't know about awk, but sed works too:

grep -v '^$' | sed ':a;N;$!ba;s/\n\([^0-9]\)/ \1/g'

Explanation of the scary thing before sed is here: How can I replace a newline (\n) using sed?