Select odd columns, then put fields of consecutive rows together
awk, bash, regex, sed, shell
Solution
Here's one way using `GNU awk`. Run like:
awk -f script.awk file.txt
Contents of `script.awk`:
{
getline line
split(line, array)
k = 6
n = ((NF - k) % 2 == 0) ? 1 : 0
for (i=1; i<=k; i++) {
printf $i OFS
}
for (j=7; j<=NF-n; j+=2) {
x = $j OFS array[j]
printf (j < NF - n) ? x OFS : x "\n"
}
}
Results:
A B C D E F 11 21 13 23 15 25 17 27
A B C D E F 31 41 33 43 35 45 37 47
A B C D E F 51 61 53 63 55 65 57 67
A B C D E F 71 81 73 83 75 85 77 87
Problem
I have a data file that I need to transform with regular expressions. More specifically, I need to maintain the first 6 columns the same, and from the 7th column on, select only the odd columns, and then put together the fields of each pair of consecutive rows. I know it sounds a bit complicated so I'll clarify this through an example. This is my original data file (it could have any number of columns): ``` A B C D E F 11 12 13 14 15 16 17 18 A B C D E F 21 22 23 24 25 26 27 28 A B C D E F 31 31 33 34 35 36 37 38 A B C D E F 41 42 43 44 45 46 47 48 A B C D E F 51 52 53 54 55 56 57 58 A B C D E F 61 62 63 64 65 66 67 68 A B C D E F 71 72 73 74 75 76 77 78 A B C D E F 81 82 83 84 85 86 87 88 ``` I figured out I can maintain the 6 first columns and then delete the odd ones with ``` awk '{for (i = 1; i <= NF; i++) if (i < 7 || i % 2 == 1) printf $i OFS}; {print ""} ``` being this the result: ``` A B C D E F 11 13 15 17 A B C D E F 21 23 25 27 A B C D E F 31 33 35 37 A B C D E F 41 43 45 47 A B C D E F 51 53 55 57 A B C D E F 61 63 65 67 A B C D E F 71 73 75 77 A B C D E F 81 83 85 87 ``` But after that I have to put together the fields of each pair of consecutive rows, like this: ``` A B C D E F 11 21 13 23 15 25 17 27 A B C D E F 31 41 33 43 35 45 37 47 A B C D E F 51 61 53 63 55 65 57 67 A B C D E F 71 81 73 83 75 85 77 87 ``` I was thinking of using sed or awk to make the whole process, since my data files are huge and I need to transform them efficiently, but I couldn't figure out a way to do the second transformation too. Any help would be highly appreciated.