I have an input like this:
FIELD1 FIELD2 FIELD3 FIELD4
aaaa bbbb cccc dddd
eeee ffff
gggg hhhh
iiii jjjj kk llll
kk
It should be a space separated list of records, but some have been wrongly written.
Correct rows are like the first row. Other rows either have a newline in the middle (second row) or a field was written as it had a newline inside a field (third field of third row).
What I would like to do is to obtain an ordered output like this:
FIELD1 FIELD2 FIELD3 FIELD4
aaaa aaaa aaaa aaaa
bbbb bbbb bbbb bbbb
cccc cccc cccc cccc
I guess that if I can fix a row like ROW2, fixing ROW3 would be similar but I can't understand how to have some tool like awk
or sed
see more than one row at a time and, for example, replace "\n\s*bbbb" with " bbbb" and fix ROW2.
EDIT:
This is a snippet of the real data I have:
G00PFMA1 transition_readonly 2 cifs 0.0.0.0/0 any
G00PFMA7 transition_export_policy_1 1
nfs 10.58.91.134 sys
G00PFMA7 transition_export_policy_1 2
nfs bmczone.tsy.fm. sys
hypovereinsbank.de
G00PFMA7 transition_export_policy_1 3
nfs inf01mz2 sys
Fields are separated by spaces. ROW1 is how every line should be, but ROW2 and ROW4 have a newline between FIELD3 and FIELD4 and ROW3 seems to have a newline inside FIELD5. This is probably due to the fact that this is the result of a copy and paste from an Excel file, where you can have a newline inside a field.
EDIT2:
With this snippet of text the correct output would be:
G00PFMA1 transition_readonly 2 cifs 0.0.0.0/0 any
G00PFMA7 transition_export_policy_1 1 nfs 10.58.91.134 sys
G00PFMA7 transition_export_policy_1 2 nfs bmczone.tsy.fm.hypovereinsbank.de sys
G00PFMA7 transition_export_policy_1 3 nfs inf01mz2 sys
cc
, why iscccc
afterwards written in the same line?cc
on the last line should be appended to the end of line 3 or to the end of the 3rd field of line 3? Are the field lengths also always constant at 4 characters?od -c -tx1 inputfile
. It may be important to see exactly how the data looks like. What program creates the input data?