2

I've large text file that looks like

some random : demo text for
illustration, can be long

and : some more

here is : another
one

I want an output like

some random : demo text for illustration, can be long
and : some more
here is : another one

I tried some strange, obviously faulty regex like %s/\w*\n/ /g but can't really get my head around.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
nlper
  • 23
  • 3

2 Answers2

2

With your shown samples, please try following awk code. Using RS(record separator), setting it nullify. This is based on your shown samples only.

awk -v RS="" '{$1=$1} 1' Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

Adding another solution in case someone is looking for printf function with awk. Though 1st solution provided in Here should be used IMHO, as an alternative adding these solutions too here.

2nd solution: Adding solution to check if lines starts with alphabets, then only add them with previous lines or so.

awk '{printf("%s%s",$0~/^[a-zA-Z]/?(FNR>1 && prev~/^[a-zA-Z]/?OFS:""):ORS,$0);prev=$0} END{print ""}' Input_file

3rd solution: Note: This will work only if your lines has colon present in the lines as per shown samples.

awk '{printf("%s%s",$0~/:/?(FNR>1?ORS:""):OFS,$0)} END{print ""}'  Input_file

Explanation: Using printf function of awk. Then using conditions, if current line has : and greater than 1 then print ORS else print nothing. If line doesn't contain : then print OFS for each line. In the END block of this program printing newline.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93