function [segment n_length n_nodes nodes] = read_bom(filenm)

%  [seg_name seg_length seg_n_nodes seg_nodes]

cmd = ['grep -e Top\ terminal\ is ' filenm '.bom > foo'];
system(cmd);
cmd = ['sed ''s/[a-zA-Z]* //g'' < foo'];
[res segment{1}] = system(cmd);
segment{1} = segment{1}(1:end-1);  %Strip last carriage return, could do this in sed if I was clever.
n_length(1) = 0;
n_nodes(1) = 1;
nodes(1) = 0;  %Placeholder, to be filled in after all elements are read.

cmd = ['grep -e ^[0-9][0-9][0-9][0-9] ' filenm '.bom > foo'];
system(cmd);

data = dlmread('foo');
N = size(data)(1);
n_length = [n_length ; data(:,3)];
n_nodes = [n_nodes ; data(:,4)];
nodes = [nodes ; data(:,7)];

fid = fopen('foo','r');

for n = 2:N+1
 line = fgetl(fid);
 [seg remain] = strtok(line);
 segment{n} = strtok(remain);
end
fclose(fid);

nodes(1) = max(nodes);

cmd = ['grep -e Bottom\ terminal\ is ' filenm '.bom > foo'];
system(cmd);
cmd = ['sed ''s/[a-zA-Z]* //g'' < foo'];
[res segment{end+1}] = system(cmd);
segment{end} = segment{end}(1:end-1);  %Strip last carriage return, could do this in sed if I was clever.
n_length(end+1) = 0;
n_nodes(end+1) = 1;
nodes(end+1) = 1;






fid = fopen([filenm '.lay'],'w');
field_width = 17;
str = sprintf('     Element             Length       Nodes');  disp(str); fprintf(fid,'%s\n',str);
fmt_str = ['%d    %-' num2str(field_width) 's     %4.1f m      %d - %d'];

for n = 1:length(segment)
  str = sprintf(fmt_str,length(segment)-n+1,segment{n},n_length(n),nodes(n),nodes(n)-n_nodes(n)+1); disp(str);fprintf(fid,'%s\n',str);
end


str = ['  Total Length = ' num2str(sum(n_length)) ' m'];  disp(str); fprintf(fid,'%s\n',str);

fclose(fid);


%
