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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print each output of loop in new column using awk or shell
# 1  
Old 06-17-2013
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 08:01 AM..
# 2  
Old 06-17-2013
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

# 3  
Old 06-17-2013
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
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

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

Print loop output on same line dynamically

Hi, I am trying to print copy percentage completion dynamically by using the script below, #!/bin/bash dest_size=0 orig_size=`du -sk $sourcefile | awk '{print $1}'` while ; do dest_size=`du -sk $destfile | awk '{print $1}'` coyp_percentage=`echo "scale=2; $dest_size*100/$orig_size"... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Print awk output in same line ,For loop

My code is something like below. #/bin/bash for i in `ps -ef | grep pmon | grep -v bash | grep -v grep | grep -v perl | grep -v asm | grep -v MGMT|awk '{print $1" "$8}'` do echo $i ORACLE_SID=`echo $line | awk '{print $2}'` USERNAME=`echo $line | awk '{print $1}'` done ============= But... (3 Replies)
Discussion started by: tapia
3 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Print for loop variable in output too

Hi, This is my input file cat input chr1:100-200 chr1:220-300 chr1:300-400 Now, I would like to run a program that will take each of the input record for i in `cat input`; do program $i | wc -l;done the output will be something like 10 20 30 But, I would like to print the... (4 Replies)
Discussion started by: jacobs.smith
4 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

Print a column with the name of the output file

I have n files and I am using cat to combine them in to one. Before that simply add the name of the output file to 4th column and then print the output. Is it possible ? input1 chr start end name 0 + key input2 chr start end name 0 + key inputn... (1 Reply)
Discussion started by: quincyjones
1 Replies

8. Shell Programming and Scripting

loop in awk - column max for each column

Hello all, this should really be easy for you... I need AWK to print column maxima for each column of such input: Input: 1 2 3 1 2 1 1 3 2 1 1 2 Output should be: 2 2 3 3 This does the sum, but i need max instead: { for(i=1; i<=NF; i++) sum +=$i } END {for(i=1; i in sum;... (3 Replies)
Discussion started by: irrevocabile
3 Replies

9. Shell Programming and Scripting

find expression with awk in only one column, and if it fits, print whole column

Hi. How do I find an expression with awk in only one column, and if it fits, then print that whole column. 1 apple oranges 2 bannanas pears 3 cats dogs 4 hesaid shesaid echo "which number:" read NUMBER (user inputs number 2 for this example) awk " /$NUMBER/ {field to search is field... (2 Replies)
Discussion started by: glev2005
2 Replies

10. UNIX Desktop Questions & Answers

Loop column output

I need help in what to do with a bash script? I'm trying to run a command to output the data from a table and then insert it into commands. Looping for each row of data. For example the output data from a table: 10 John house 20 Jane apt 30 Joe townhomeThen I need to take the output... (1 Reply)
Discussion started by: handband2
1 Replies
Login or Register to Ask a Question