Issue
I have two tables in a same file
**Table A**
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
**Table B**
a b c d e
f g h i j
k l m n o
1 a 2 b 3 c 4 d 5 e
6 f 7 g 8 h 9 i 10 j
11 k 12 l 13 m 14 n 15 o
I am not sure what the right word for this kind of merging or combining is? I tried 'cascading' and 'merging in between' I got zero output from google. Sorry english is not my first language
Any guidance would be greatly appreciated.
My first plan is to make two different text files and combine them using column identifiers. but if there is a n easier way please let me know. Thanks
Solution
Awk ./merge-tables.awk
script:
#! /usr/bin/awk -f
BEGIN {
# Set Output Field Separator
OFS = "\t"
}
# New table
/^\*\*.*\*\*$/ {
# Increment table number
ntable += 1
# RAZ line number
nline = 0
# Next line
next
}
# Ignore empty/blanks line
/^ *$/ {
next
}
# Memorise first table
ntable == 1 {
i = 1
nline += 1
while (i <= NF) {
table[nline "/" i] = $i
i += 1
}
}
# Print first table element followed by second table element
ntable == 2 {
i = 1
nline += 1
while (i <= NF) {
e1 = table[nline "/" i]
e2 = $i
printf("%s%s%s%s", e1, OFS, e2, (i<NF)?OFS:"")
i += 1
}
print ""
}
Executable with:
chmod +x ./merge-tables.awk
Run like that:
./merge-tables.awk tables.txt
Output:
1 a 2 b 3 c 4 d 5 e
6 f 7 g 8 h 9 i 10 j
11 k 12 l 13 m 14 n 15 o
New version
Difference with previous: Ignore table name (surrounded or not with **
). The empty line is considered as the table separator
#! /usr/bin/awk -f
BEGIN {
# Set Output Field Separator
OFS = "\t"
ntable += 1
}
# Ignore empty/blanks line
/^ *$/ {
ntable += 1
# RAZ line number
nline = 0
# Next line
next
}
# Squeeze first line (table name)
ntable == 1 && nline == 0 {
nline += 1
next
}
# Memorise first table
ntable == 1 {
i = 1
nline += 1
while (i <= NF) {
table[nline "/" i] = $i
i += 1
}
}
# Squeeze first line (table name)
ntable == 2 && nline == 0 {
nline += 1
next
}
# Print first table element followed by second table element
ntable == 2 {
i = 1
nline += 1
while (i <= NF) {
e1 = table[nline "/" i]
e2 = $i
printf("%s%s%s%s", e1, OFS, e2, (i<NF)?OFS:"")
i += 1
}
print ""
}
Answered By - Arnaud Valmary Answer Checked By - Mary Flores (WPSolving Volunteer)