Create new rows for each column value with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create new rows for each column value with awk
# 1  
Old 04-20-2015
Create new rows for each column value with awk

Hi,
I have the following type of data that is separated by tabs:

Code:
-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:


Code:
-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
Code:
awk

that would permit this type of action?
Simple writing out what I need to print on each line à la
Code:
$awk '{print $1, $2, etc}'

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

So far, I have tried out some
Code:
awk

solutions that help to solve problems regarding transposing the data, such as this:
Code:
awk '{ for (i=1;i<=NF;i++ ) printf $i " " }'

and

Code:
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 because I want each column to also be attached to the original value of its row in Column 1.
Any ideas?

Last edited by owwow14; 04-20-2015 at 06:48 AM.. Reason: added info regarding trials.
# 2  
Old 04-20-2015
Hi,
Try:
Code:
 awk '{for (i=2;i<=NF;i++ ) print $1,"\t",$i}' file

Regards.
This User Gave Thanks to disedorgue For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare values in multiple rows in one column using awk

I would like to compare values in column 8, and grep the ones where the different is > 1, columns 1 and 2 are the key for array. Every 4 rows the records values in columns 1 and 2 changed. Then, the comparison in the column 8 need to be done for the 4 rows everytime columns 1 and 2 changed ... (4 Replies)
Discussion started by: jiam912
4 Replies

2. Shell Programming and Scripting

Convert rows into columns and create table with awk

Hello I've four fields . They are First Name, Last Name, Age, Country. So when I run a Unix command, I get below output with these fields comes every time in different order as you can see. Some times first name is the first row and other time last name is first row in the output and etc etc..... (9 Replies)
Discussion started by: rprpr
9 Replies

3. UNIX for Advanced & Expert Users

Conversion of rows to columns using awk based om column value

HI, My Input file data is dn:adcfgeneral id:13343 Name:xxxxxx Password:iutyerwuitywue wpuwt tuiytruityrutyrwtyrwp dn:cdferwjyyyy id:3875 Name:yyyy Password :hgfdsjkfhdsfkdlshf dshfkldshfdklsfh interset:uiuiufj My output should be ... (6 Replies)
Discussion started by: dineshaila
6 Replies

4. Shell Programming and Scripting

awk split columns after matching on rows and summing the last column

input: chr1 1 2 3 chr1 1 2 4 chr1 2 4 5 chr2 3 6 9 chr2 3 6 10 Code: awk '{a+=$4}END{for (i in a) print i,a}' input Output: chr112 7 chr236 19 chr124 5 Desired output: chr1 1 2 7 chr2 3 6 19 chr1 2 4 5 (1 Reply)
Discussion started by: jacobs.smith
1 Replies

5. Shell Programming and Scripting

Awk: Interchange the Rows and column

Hi, I have following input and want to change it to following output INPUT 01-APR-14,KB,822714 01-APR-14,MB,8133431 02-APR-14,KB,757140 02-APR-14,MB,7770368 03-APR-14,KB,815427 03-APR-14,MB,7590511 04-APR-14,MB,7529895 04-APR-14,KB,779561 05-APR-14,MB,8151537 05-APR-14,KB,809675 ... (6 Replies)
Discussion started by: siramitsharma
6 Replies

6. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

8. Shell Programming and Scripting

awk transpose rows to column

Need to transpose in awk rows to column like this: input: A1,6,5,4 3,2,1, A2,8,7,9,10,11,12,13,14 A3,1,2,3,5,7,8,9 A4,9,4,8,1,5,3, output: A1,1 A1,2 A1,4 ... A2,7 A2,8 ... A3,1 A3,2 ... A4,1 A4,3 (5 Replies)
Discussion started by: sdf
5 Replies

9. UNIX for Dummies Questions & Answers

how to count number of rows and sum of column using awk

Hi All, I have the following input which i want to process using AWK. Rows,NC,amount 1,1202,0.192387 2,1201,0.111111 3,1201,0.123456 i want the following output count of rows = 3 ,sum of amount = 0.426954 Many thanks (2 Replies)
Discussion started by: pistachio
2 Replies

10. Shell Programming and Scripting

awk to select rows based on condition on column

I have got a file like this 003ABC00281020091005000100042.810001 ... (8 Replies)
Discussion started by: Maruti
8 Replies
Login or Register to Ask a Question