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?
# 1  
Old 03-05-2015
Awk: is it possible to print into multiple columns?

Hi guys,

I have hundreds file like this, here I only show two of them:
file 1
Code:
feco4_s_BB95.log                                    ZE_1=-1717.5206260
feco4_t_BB95.log                                    ZE_1=-1717.5169250
feco5_s_BB95.log                                    ZE_1=-1830.9322060

file 2
Code:
feco4_s_BBRC.log                                    ZE_1=-1717.2394670
feco4_t_BBRC.log                                    ZE_1=-1717.2382150
feco5_s_BBRC.log                                    ZE_1=-1830.6170990

can I get an output like this? which is simple for copying into excel.
Code:
feco4_s_BB95.log        -1717.5206260 -1717.2394670
feco4_t_BB95.log        -1717.5169250 -1717.2382150
feco5_s_BB95.log        -1830.9322060 -1830.6170990

Thank you very much for your kind help!

Zhen
# 2  
Old 03-05-2015
The order of results is not guaranteed:

Code:
{ sub(/ZE_1=/,""); A[$1]=A[$1]" "$2 } END { for(X in A) print X, substr(A[X],1) }' inputfile

# 3  
Old 03-05-2015
Quote:
Originally Posted by Corona688
The order of results is not guaranteed:

Code:
{ sub(/ZE_1=/,""); A[$1]=A[$1]" "$2 } END { for(X in A) print X, substr(A[X],1) }' inputfile

Thanks. But the order should consistent with the file name, let say the second column should be the numbers from the file2, and so the hundredth column should be from the file100. ortherwise, these numbers are not in order and are becoming meaningless.

could it possibly be processed with cycles?
# 4  
Old 03-05-2015
Are your first column always the same and in same order?
If yes, this might help extract col2 from all your files , but you need to paste col1 from one of your files.

Should maintain order.


Code:
BEGIN {
  count = 1
}

FNR == 1 && FNR < NR {
  count++
}

{
  if ( _[FNR] ) {
    while ( c[FNR] != count-1 ) {
      _[FNR] = (_[FNR] " " sprintf("%8s",""))
      c[FNR]++
    }
    _[FNR] = (_[FNR] " " sprintf("%-8s",$2))
    c[FNR]++
  }
  else if ( count == 1 ) {
    _[FNR] = sprintf("%-8s",$2)
    c[FNR]++
  }
  else {
    while ( c[FNR] != count-1 ) {
      if ( _[FNR] )
        _[FNR] = (_[FNR] " " sprintf("%8s",""))
      else
        _[FNR] = sprintf("%8s","")
      c[FNR]++
    }
    _[FNR] = (_[FNR] " " sprintf("%-8s",$2))
    c[FNR]++
  }
}

END {
  for (i=1; i<=length(_); i++)
    print _[i]
}


Save this as liuzhencc.awk , give a execute status like chmod 777

and run as

Code:
awk -f liuzhencc.awk file1 file2 .... filen

# 5  
Old 03-05-2015
Quote:
Originally Posted by liuzhencc
Thanks. But the order should consistent with the file name, let say the second column should be the numbers from the file2, and so the hundredth column should be from the file100. ortherwise, these numbers are not in order and are becoming meaningless.

could it possibly be processed with cycles?
Are they sorted, and guaranteed not missing any rows? You could just use paste.
# 6  
Old 03-05-2015
I think it works with the following code, but in very an inefficient way.
Code:
/*add the file name to the first row*/
for i in file*; do
printf "%20s" ${i} >> $i.txt;
done

printf "\n" >> ../$i.txt;

/*print the numbers of the second row from each file $i*/
for ((j=1;j<=11;j++)); do
for i in file*; do
Num=$(awk "NR==${j}" ${i} | awk -F= '{print $2}');
printf "%20.7f" "${Num}" >> $i.txt;
done;

printf "\n" >> $i.txt;

done;

# 7  
Old 03-05-2015
Are they sorted, and guaranteed not missing any rows? You could just use paste.
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