wanaka
wanaka

Reputation: 1

shell script to convert rows to columns

I have a text file with the following format

 Type: XTREMIO
  UID: 51,4f,0c,51,9d,00,05,22
  Vendor: XtremIO
  Product: XtremApp
  Size: 10.00 GB
  Name: knocknarea_jounal
  Replication info: [rpa_A, RPA_B, knocknarea_jounal]
  Array Serial: CKM00150400153
  RPA To volume paths map: None



 Type: XTREMIO
  UID: 51,4f,0c,51,9d,00,05,28
  Vendor: XtremIO
  Product: XtremApp
  Size: 5.00 GB
  Name: test_rep
  Replication info: N/A
  Array Serial: CKM00150400153
  RPA To volume paths map: None

My objective is to convert those block into row like this with a delimiter:

XTREMIO;51,4f,0c,51,9d,00,05,28;XtremIO;XtremApp;5.00 GB;test_rep;CKM00150400153

Upvotes: 0

Views: 216

Answers (1)

Pierre François
Pierre François

Reputation: 6061

Normally, you should post the code you have written up to know, even if it is not working, so that we can help you instead of working in place of you.

However, there is trivial solution calling awk twice. Assuming your input file is called input, you could try/

awk 'BEGIN{FS = ": "}{print $2}' input | awk 'BEGIN{RS = ""; FS = "\n"; OFS = ";"}{print $1, $2, $3, $4, $5, $6, $8}'

Output:

XTREMIO;51,4f,0c,51,9d,00,05,22;XtremIO;XtremApp;10.00 GB;knocknarea_jounal;CKM00150400153
XTREMIO;51,4f,0c,51,9d,00,05,28;XtremIO;XtremApp;5.00 GB;test_rep;CKM00150400153

Explanation: the first instance of awk is removing everything in the begin of each line until and including the string ": ".

The second one is using the paragraph mode of awk setting the record separator RS to the null string, which means that a record is each set of lines with no blank line, and sets the field separator FS to a newline character. It reads records and outputs the fields as they are, but changing the field separator and the record separator into resp. ";" and a single newline (option by default for ORS).

Upvotes: 1

Related Questions