Print each output of loop in new column using awk or shell | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Print each output of loop in new column using awk or shell

UNIX for Dummies Questions & Answers


Tags
awk, bash, linux, shell

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-17-2013
maryre89 maryre89 is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 3 October 2013, 2:21 PM EDT
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Print each output of loop in new column using awk or shell

I have this output from a loop


Code:
  
    a11
    1,2
    3,4
    5,6
    7,8
    12,8
    5,4
    3,6

    a12
    10,11
    12,13
    15,18
    20,22

    a13
    ...

but i want this output:


Code:
  a11   a12    a13
  1,2   10,11   ...
  3,4   12,13
  5,6   15,18
  7,8   20,22
  12,8
  5,4
  3,6

It means i want every output of loop in new column
how do i do it???

Last edited by maryre89; 06-17-2013 at 07:01 AM..
Sponsored Links
    #2  
Old 06-17-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 29 August 2014, 5:55 PM EDT
Location: Galactic Empire
Posts: 3,362
Thanks: 230
Thanked 1,197 Times in 1,125 Posts
Using awk:

Code:
awk '
        $1 ~ /^a/ {
                c = 0
                c += 1
                A[c] = A[c] ? A[c] OFS $1 : $1
                m = ( m < c ? c : m )
        }
        $1 !~ /^a/ {
                c += 1
                A[c] = A[c] ? A[c] OFS $1 : $1
                m = ( m < c ? c : m )
        }
        END {
                for ( i = 1; i <= m; i++ )
                        print A[i]
        }
' OFS='\t' file

Sponsored Links
    #3  
Old 06-17-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 20 August 2014, 7:37 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts

Code:
awk '/^a/{if(x>max){max=x};x=1} 
{A[x++]=A[x]?A[x] OFS $0 : $0} 
END{for(i=1;i<max;i++){print A[i]}}' OFS="\t" file

    #4  
Old 06-17-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 1 September 2014, 12:23 PM EDT
Location: Aachen, Germany
Posts: 4,158
Thanks: 69
Thanked 994 Times in 942 Posts
Try
Code:
awk '/^a/ {fn++}  {print >fn}' fn=0 file | paste {1..3}
a11    a12    a13
1,2    10,11    ...
3,4    12,13    
5,6    15,18    
7,8    20,22    
12,8        
5,4        
3,6

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print for loop variable in output too jacobs.smith Shell Programming and Scripting 4 02-20-2013 02:41 PM
awk command to print only selected rows in a particular column specified by column name ks_reddy Shell Programming and Scripting 4 09-01-2012 11:32 AM
Print a column with the name of the output file quincyjones Shell Programming and Scripting 1 03-06-2012 07:25 PM
find expression with awk in only one column, and if it fits, print whole column glev2005 Shell Programming and Scripting 2 01-26-2010 07:56 PM
Loop column output handband2 UNIX Desktop for Dummies Questions & Answers 1 11-03-2008 09:46 PM



All times are GMT -4. The time now is 02:34 PM.