Since you didn't use code tags, I'm not sure that I have correctly interpreted what your input looks like, but this might work:
awk '
/=/ { if(hc) f[hc] = s
h[++hc] = last
last = s = ""
lc = fc = 0
next
}
{ if(lc) {
if(fc) s = s " " last
else { fc = 1
s = last
}
}
lc = 1
last = $0
}
END { for(i = 1; i <= hc; i++) printf("%s%s", h, i == hc ? "\n" : "|")
if(lc && sc) f[hc] = s " " last
else f[hc] = last
for(i = 1; i <= hc; i++) printf("%s%s", f, i == hc ? "\n" : "|")
}' data
As always, if you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk .
PLEASE use code tags for code and data as required by forum rules!
This has become uglier than what I went for, but at least for your special problem, it might work:
awk '$1=="=" {header = header M1 "|" # if "=" encountered, use "dragging" copy, i.e. last line's $1
row = row "|" # insert "|" separator into row
next # no further action on this line
}
{row = row " " $1; M1 = $1} # collect everything into row; keep a "dragging" copy of $1 in M1
END {gsub (/ [^ ]*\| /, "|", row) # remove header fields from row
sub (/^\|/, "", row) # remove leading "|" from row
sub (/\|$/,"", header) # remove trailing "|" from header
print header; print row} # print it
' file
LAC|DN|EQN|CAT|OPTRCL|TRARSTR|LNATT|COS
040|24001001|920- 2- 0- 1|MS|3|ACTTRACL TRACLACT TRACLMOD|PB|CLIP
---------- Post updated at 11:21 ---------- Previous update was at 10:57 ----------
Well, this might be a bit more straightforward and easier to understand...
awk ' {tot = tot " " $1} # collect everything into one long line
END {gsub (/ [^ ]* =/, "|&", tot) # separate entries by "|"
n = split (tot, Ar1, "\| ") # split line by "|", remember field count
for (i=2; i<=n; i++) # every field but first (artificial, empty field)
{split (Ar1, Ar2, " = ") # split into header/row part
head = head (i>2?"|":_) Ar2[1] # and create header
row = row (i>2?"|":_) Ar2[2] # and row (both avoiding leading "|")
}
print head; print row # print both
}
' file
import re
arr=[]
col=[]
val=[]
with open("a.txt","r") as f:
for line in f:
line=re.sub("\n","",line)
if line.find("=")<0:
arr.append(line)
else:
if len(arr)==1:
col.append(arr[0])
else:
value=" ".join(arr[:-1])
val.append(value)
col.append(arr[-1])
arr=[]
val.append(arr[0])
print(" | ".join(col))
print(" | ".join(val))