Reformatting a list to table

Hi!

I have a list with a lot of records that I need to work with. The problem is that the list is populated successive one record at the time in a text file, and to gain anything from these records I need them to be put out in a table.

This is an example of what the list looks like:

(145) To: One From: Two Date: 21.03.2012
This is text
And so is this

(276) To: Three From: Two Date: 22.03.2012
This  is another text

(13) To: Two From: One Date: 22.03.2012
And so
is 
this

What I would like is to have this text formatted like this:

(145) To: One From: Two Date: 21.03.2012;This is text;And so is this
(276) To: Three From: Two Date: 22.03.2012;This  is another text
(13) To: Two From: One Date: 22.03.2012;And so;is;this

As you can see, each record starts with (integer), and is followed by one or more lines of text. I have started on a bash script to do this, but I think I have a problem removing the newline-characters. :wall:

Does anyone have an idea of how I can fix this?

awk 'END {
  if (r) print r
  }
/^\([0-9]+\)/ {
  print r; r = x
  }
{ 
  r = r ? r OFS $0 : $0 
  }' OFS=\; infile

On Solaris use nawk or /usr/xpg4/bin/awk.

1 Like

Alternatively, try this:

awk '$1=$1' FS='\n' OFS=';' RS= infile
1 Like

Of course,
if the records are always separated by blank lines
(just like in the sample input file).

Yes, indeed. Completely blank lines are a prerequisite, otherwise the simple version will not work. So unlike Radoulov's version, it is not very robust.

Radulovs example works like a charm! I haven't got my really long long file at home for the moment, so I'm not sure if there really are blank lines between all the records. I'll try both examples at work tomorrow, and keep them until next time.

Thanks a heap, guys! :b: