Repeat line convert upper line

HI Guys,

My Input :-

R3 AV44 50 0 100 100 100
R3 AV44 1 0 100 100 100
R3 AV45 50 0 100 100 100
R3 AV45 0 3 100 100 100
R3 AV45S 50 0 100 100 100
R3 AV45S 0 4 100 100 100

Output :-

R3 AV44 50 0 100 100 100 1 0 100 100 100
R3 AV45 50 0 100 100 100 0 3 100 100 100
R3 AV45S 50 0 100 100 100 0 4 100 100 100

Repeat data base on column 2 move with upper line.

An awk approach:

awk '
        {
                V = $0
                sub ( /[^ ]*[ ][^ ]*[ ]/, X, V )
                if ( !(A[$1,$2]) )
                        A[$1,$2] =  V
                else
                        A[$1,$2] = A[$1,$2] OFS V
        }
        END {
                for ( k in A )
                        print k, A[k]
        }
' file

can we convert to one line command :slight_smile: If possible

Why don't you show us what one liner(s) you have tried...

Hi,

$ cat file.txt
R3 AV44 50 0 100 100 100
R3 AV44 1 0 100 100 100
R3 AV45 50 0 100 100 100
R3 AV45 0 3 100 100 100
R3 AV45S 50 0 100 100 100
R3 AV45S 0 4 100 100 100
$ paste -d' ' - - <file.txt | cut -d' ' -f1-7,10-
R3 AV44 50 0 100 100 100 1 0 100 100 100
R3 AV45 50 0 100 100 100 0 3 100 100 100
R3 AV45S 50 0 100 100 100 0 4 100 100 100

Regards.

Both Solution's are perfect :slight_smile:

Try

awk '{x=$0; getline; $1=$2=""; $0=x $0}1' file
R3 AV44 50 0 100 100 100  1 0 100 100 100
R3 AV45 50 0 100 100 100  0 3 100 100 100
R3 AV45S 50 0 100 100 100  0 4 100 100 100

EDIT: If you need to get rid of the double space, add ; gsub (/ */," ") before the closing bracket.

One more approach

$ awk '{printf(!_[$2]++)?$0 FS:($1=$2="")$0 RS}' test.txt
R3 AV44 50 0 100 100 1 0 100 100 100
R3 AV45 50 0 100 100 0 3 100 100 100
R3 AV45S 50 0 100 100 0 4 100 100 100
1 Like

That's nice: assignment in conditional assignement!

And a sed version:

sed 'N; s/\n *[^ ]* *[^ ]*//'  file