Search code examples
awksedterminalrowcol

Creating new line for each n-column value, while maintaining Col. 1 value


I have the following type of data that is separated by tabs (n-columns):

-2  abandonar   abandono    abandonas   abandona    abandonamos abandonáis  abandonan   
-4  abandonado  abandonada  abandonados abandonadas 
-2  abandona    abandonos                                       
-3  secuestrado secuestrada secuestrados    secuestradas    secuestraría    secuestrarías   secuestraríamos secuestraríais  secuestrarían

I need to keep the values in Column 1, but I need each string in the following columns to be moved to their own line. The desired result would be this:

-2  abandonar
-2  abandono
-2  abandonas
-2  abandona
-2  abandonamos
-2  abandonáis
-2  abandonan   
-4  abandonado
-4  abandonada
-4  abandonados
-4  abandonadas 
-2  abandona    
-2  abandonos                                       
-3  secuestrado 
-3  secuestrada 
-3  secuestrados    
-3  secuestradas    
-3  secuestraría    
-3  secuestrarías   
-3  secuestraríamos
-3  secuestraríais
-3  secuestrarían

Is there a function in awk that would permit this type of action? Simple writing out what I need to print on each line à la:

$awk '{print $1, $2, etc}'

would be very cumbersome, especially because there are Code: n-columns

So far, I have tried out some awk solutions that help to solve problems regarding transposing the data. I found this solutions here:

awk '{ for (i=1;i<=NF;i++ ) printf $i " " }'

and

awk '{
       for (f = 1; f <= NF; f++) { a[NR, f] = $f } 
     }
     NF > nf { nf = NF }
     END {
       for (f = 1; f <= nf; f++) {
           for (r = 1; r <= NR; r++) {
               printf a[r, f] (r==NR ? RS : FS)
           }
       }
    }' input

However, they do not exactly solve the problem, as you can probably see, because I want each column to also be attached to the original value of its row in Column 1 and these solutions just transpose the data. Any ideas of how I achieve my desired result?


Solution

  • If I understand you correctly, then

    awk '{ for(i = 2; i <= NF; ++i) print $1, $i }' filename
    

    should work.