Awk: is it possible to print into multiple columns?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: is it possible to print into multiple columns?
# 8  
Old 03-05-2015
Quote:
Originally Posted by Corona688
Are they sorted, and guaranteed not missing any rows? You could just use paste.
I cannot get it work in bash. the error message:
Code:
-bash: syntax error near unexpected token `/ZE_1=/,""'

# 9  
Old 03-05-2015
My program, on reflection, isn't even close to what you want anyway.

paste might be closer, if your data is all in the same order and guaranteed not missing any rows.

Is it?

Please answer my question. It's relevant.
This User Gave Thanks to Corona688 For This Post:
# 10  
Old 03-05-2015
yes. the data for all formatted in same order and not missing any rows and any column.
Sorry for my bad English and I didn't read the question.
will it be possible to give an output as below?
Code:
                                         file1                   file2
feco4_s_BB95.log        -1717.5206260 -1717.2394670
feco4_t_BB95.log        -1717.5169250 -1717.2382150
feco5_s_BB95.log        -1830.9322060 -1830.6170990

# 11  
Old 03-05-2015
I will do this in 3 steps.

1. Extracting the first column from any of the files, add a blank line and store in a file.
Code:
awk 'NR==1{print ""} {print $1}'  file1 > ../tmpcol1

2. Paste all col2 side by side from all files with file-names as headers.

Code:
awk '
            FNR == 1 {
                    c += 1
                    d = FILENAME
                    sub ( /\/.*/, X, d )
                    D[c] = d
            }
            {
                    A[c,FNR] = $2
                    m = m < FNR ? FNR : m
            }
            END {
                    while ( ++k <= c )
                            printf "%s\t", D[k]
                    printf "\n"
    
                    while ( ++j <= m )
                    {
                            while ( ++i <= c )
                            {
                                    printf "%s\t", A[i,j]
                            }
                            printf "\n"
                            i = 0
                    }
            }
'  * > allcol2

3. Paste the 1st column from step 1 and delete it.

Code:
paste ../tmpcol1 allcol2 > final_file
rm ../tmpcol1

This User Gave Thanks to senhia83 For This Post:
# 12  
Old 03-05-2015
Thanks! it does the trick!

---------- Post updated at 02:26 AM ---------- Previous update was at 02:19 AM ----------

Quote:
Originally Posted by Corona688
My program, on reflection, isn't even close to what you want anyway.

paste might be closer, if your data is all in the same order and guaranteed not missing any rows.

Is it?

Please answer my question. It's relevant.
Thanks Corona688! yes, "Paste" also does the trick very effectively. This functional is exactly what I expected at the very beginning, which could paste the column from multiple files into a table sheet. However, I didn't know there is a command like "Paste". Now, everything become simple. Thanks again for your help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print multiple required columns dynamically in a file using the header name?

Hi All, i am trying to print required multiple columns dynamically from a fie. But i am able to print only one column at a time. i am new to shell script, please help me on this issue. i am using below script awk -v COLT=$1 ' NR==1 { for (i=1; i<=NF; i++) { ... (2 Replies)
Discussion started by: balu1234
2 Replies

2. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

3. UNIX for Beginners Questions & Answers

Print multiple columns in scientific notation

Hi everybody, I have file 1 with 15 columns, I want to change the formatting of the numbers of columns 10,11 and 12 in the scientific notation. I used the Following script: awk '{print $10}' file1.dat | awk '{printf "%.2e\n", $1}' > file2.dat awk '{print $11}' file1.dat | awk '{printf... (7 Replies)
Discussion started by: supernono06
7 Replies

4. UNIX for Beginners Questions & Answers

Count multiple columns and print original file

Hello, I have two tab files with headers File1: with 4 columns header1 header2 header3 header4 44 a bb 1 57 c ab 4 64 d d 5 File2: with 26 columns header1.. header5 header6 header7 ... header 22...header26 id1 44 a bb id2 57 ... (6 Replies)
Discussion started by: nans
6 Replies

5. Shell Programming and Scripting

awk print columns and variable

Hi, Can anyone help with the below please? I have written some code which takes an input file, and and prints the contents out to a new file - it then loops round and prints the same columns, but increments the ID column by 1 each time. Input file; NAME,1,15-Dec-15, NAME,1,21-Dec-15,... (9 Replies)
Discussion started by: Ads89
9 Replies

6. Linux

Find and print in multiple columns

Hi all, My input file is : 0 13400000 sil 13400000 14400000 a 14400000 14900000 dh 14900000 15300000 a 15300000 16500000 R 16500000 17000000 k 17000000 17300000 u 17300000 17600000 th 17600000 17900000 sil 17900000 18400000 th 18400000 18900000 a 18900000 19600000 g 19600000 19900000... (1 Reply)
Discussion started by: girlofgenuine
1 Replies

7. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

8. Shell Programming and Scripting

Removing columns from awk '{ print $0 }'

I have a one-line command, lsusb | awk '{ $1=""; $2=""; $3=""; $4=""; $5=""; $6=""; print $0 }' It works, and gives the results I expect, I was just wondering if I am missing some easier way to nullify the first 6 column variables? Something like, lsusb | awk '{ $(1-6)=""; print $0 }' But... (10 Replies)
Discussion started by: AlphaLexman
10 Replies

9. Shell Programming and Scripting

awk command to print multiple columns

Hello Team, I have written following command which is giving output is as shown below. bash-3.00$ grep -i startup catalina.out | tail +2 | sed -n 1p | awk -F" " '{ for (x=1; x<=5; x++) { printf"%s\n", $x } }' Dec 19, 2010 3:28:39 PM bash-3.00$ I would like to modify above command to... (2 Replies)
Discussion started by: coolguyamy
2 Replies

10. Shell Programming and Scripting

cannot print the columns i want with awk.

hi friends! i have a script where a execute a veritas command, available_media wich retrieves me a list of tapes .lst then i execute cat /tmp/listtapes.lst | grep -v VL |sed '/^$/d'|awk -F, '{print $1, $3, $4, $9} ' > /tmp/media1.lst but it prints all the columns instead of the four... (3 Replies)
Discussion started by: pabloli150
3 Replies
Login or Register to Ask a Question