Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine multiple columns of different lengths into one column in BASH

Tags:

bash

shell

awk

I need to combine columns of different lengths into one column using BASH. Here is an example input file:

11  1 2   3 4   5 6   7 8
12  1 2   3 4   5 6   7 8
13  1 2   3 4   5 6   7 8
14  1 2         5 6   7 8
15  1 2               7 8

And my desired output:

1
1
1
1
1
3
3
3
5
5
5
5
7
7
7
7
7

The input data is pairs of columns as shown. Each pair is separated from another by a fixed number of spaces. Values within a pair of columns are separated by one space. Thanks in advance!

like image 464
siddarthkaki Avatar asked Jan 25 '26 19:01

siddarthkaki


1 Answers

Using GNU awk for fixed width field handling:

$ cat file
1 2   3 4   5 6   7 8
1 2   3 4   5 6   7 8
1 2   3 4   5 6   7 8
1 2         5 6   7 8
1 2               7 8

$ cat tst.awk                          
BEGIN{ FIELDWIDTHS="1 1 1 3 1 1 1 3 1 1 1 3 1 1 1" }
{
    for (i=1;i<=NF;i++) {
        a[NR,i] = $i
    }
}
END {
    for (i=1;i<=NF;i+=4)
        for (j=1;j<=NR;j++)
            if ( a[j,i] != " " ) 
                print a[j,i]
}

$ gawk -f tst.awk file
1
1
1
1
1
3
3
3
5
5
5
5
7
7
7
7
7
like image 126
Ed Morton Avatar answered Jan 27 '26 08:01

Ed Morton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!