bernie
bernie

Reputation: 823

File column manipulation using awk sed

I have 2 files that I want combined as below. FILE1:

AAA 
1234 
BBB
2341

FILE2:

AAA
9876
67 89 01
BBB
4567
23 45 23

Final file required

AAA 1234 9876 67 89 01
BBB 2341 4567 23 45 23

How do I achieve this in awk or sed or both?

Upvotes: 0

Views: 432

Answers (3)

Fredrik Pihl
Fredrik Pihl

Reputation: 45644

Pure awk:

/^[A-Z]/ {
    token=$1
}
/^[0-9]/{
    for (i=1; i<=NF; i++) {
        C[token]=C[token] " " $i
    }
}
END {
    for (i in C) {
        print i, C[i]
    }
}

Output:

$ awk -f f.awk f1 f2
AAA  1234 9876 67 89 01
BBB  2341 4567 23 45 23

Can be shortened to a 3-liner:

/^[A-Z]/ { token=$1 }
/^[0-9]/ { C[token]=C[token] " " $0 }
END { for (i in C) { print i, C[i] } }

Upvotes: 3

Karl Nordstr&#246;m
Karl Nordstr&#246;m

Reputation: 327

transform.awk

{key=($1 ~/[A-Z]+/)}

key==1 {
if(NR>1) {printf "\n"}
printf "%s",$1}

key==0 {printf " %s",$0}

END {printf "\n"}

This will transform the file without assuming a fixed number of entries, but rather that the key confirm to a certain pattern as given by the regular expression in the first row. In this case a stretch of capital letters.

join <(awk -F transform.awk file1) <(awk -F transform.awk file2)

Upvotes: 0

nick
nick

Reputation: 663

You can try this ( perhaps it's to large solution, but it work ):

a.awk:

function print_stuff( start_string, end_string, file2 )
{
    printf "%s ", start_string

    getline 

    while ( $0 != end_string )
    {
        for ( i = 1; i < NF + 1; i++ )
        {
            printf "%s ", $i    
        }

        if ( getline <= 0 )
        {
            break    
        }
    }

    while ( $0 != start_string )
    {
        if ( ( getline < file2 ) <= 0 )
        {
            break    
        }
    }

    getline < file2

    while ( $0 != end_string )
    {
        for ( i = 1; i < NF + 1; i++ )
        {
            printf "%s ", $i    
        }

        if ( ( getline < file2 ) <= 0 )
        {
            break    
        }
    }

    printf "\n"

    close( file2 )
}


BEGIN { file2 = "file2"; aaa = "AAA"; bbb = "BBB" }

aaa { print_stuff( aaa, bbb, file2 ) }
bbb { print_stuff( bbb, "",  file2 ) }

run: awk -f a.awk file1

output:

AAA 1234 9876 67 89 01 
BBB 2341 4567 23 45 23

Upvotes: 1

Related Questions