0

I want to print a particular column (in this case column 7) of my file in scientific notation, whereas rest of the columns get printed as is.

How can I selectively use printf only for column 7 and just print for first 6 columns?

example input:

C   6   12.011  0.51    3.56E-01    4.60E-01    0.458399
CA  6   12.011  -0.15   3.55E-01    2.93E-01    0.291708
CAI 6   12.011  -0.25   3.55E-01    3.05E-01    0.30421
CC  6   12.011  0.62    3.56E-01    2.93E-01    0.291708

desired output:

C   6   12.011  0.51    3.56E-01    4.60E-01    4.58E-01
CA  6   12.011  -0.15   3.55E-01    2.93E-01    2.92E-01
CAI 6   12.011  -0.25   3.55E-01    3.05E-01    3.04E-01
CC  6   12.011  0.62    3.56E-01    2.93E-01    2.92E-01
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
ruchi
  • 21
  • Are your columns fixed width? Is it always the last column that you want to change? – Tom Fenech Sep 19 '16 at 16:06
  • awk '{print $1, $2, $3, $4, $5, $6}' input.txt > temp1 awk '{printf "%4.3e" $7 }' input.txt > temp2 I get error: awk: (FILENAME=input.txt FNR=1) fatal: not enough arguments to satisfy format string `%4.3e0.458399' I thought I will paste temp1 temp2 > output.txt However, I will prefer if I could do all this in one step/command – ruchi Sep 19 '16 at 16:11
  • No, its not always the last column that I need to modify to scientific notation. – ruchi Sep 19 '16 at 16:12
  • please, no code in comments. A good Q will show sample input, required output AND current code. As people ask Qs about your problem, please consider editing your Q to improve its quality so readers don't have to trudge thru an increasingly long trail of hard to read comments. Good luck. – shellter Sep 19 '16 at 18:14

2 Answers2

2

You can use sprintf:

$ awk -v OFS="\t" '{ $7 = sprintf("%.2E", $7) }1' input.txt
C   6   12.011  0.51    3.56E-01    4.60E-01    4.58E-01
CA  6   12.011  -0.15   3.55E-01    2.93E-01    2.92E-01
CAI 6   12.011  -0.25   3.55E-01    3.05E-01    3.04E-01
CC  6   12.011  0.62    3.56E-01    2.93E-01    2.92E-01
jas
  • 10,715
  • 2
  • 30
  • 41
0

To change only the last column without affecting spacing of other fields:

$ cat ip.txt 
C   6   12.011  0.51    3.56E-01    4.60E-01    0.458399
CA  6   12.011  -0.15   3.55E-01    2.93E-01    0.291708
CAI 6   12.011  -0.25   3.55E-01    3.05E-01    0.30421
CC  6   12.011  0.62    3.56E-01    2.93E-01    0.291708

$ perl -pe 's/\S+$/sprintf "%.2E", $&/e' ip.txt 
C   6   12.011  0.51    3.56E-01    4.60E-01    4.58E-01
CA  6   12.011  -0.15   3.55E-01    2.93E-01    2.92E-01
CAI 6   12.011  -0.25   3.55E-01    3.05E-01    3.04E-01
CC  6   12.011  0.62    3.56E-01    2.93E-01    2.92E-01

If some other column is required:

$ perl -pe 's/^(\S+\s+){2}\K\S+/sprintf "%.2E", $&/e' ip.txt 
C   6   1.20E+01  0.51    3.56E-01    4.60E-01    0.458399
CA  6   1.20E+01  -0.15   3.55E-01    2.93E-01    0.291708
CAI 6   1.20E+01  -0.25   3.55E-01    3.05E-01    0.30421
CC  6   1.20E+01  0.62    3.56E-01    2.93E-01    0.291708
Sundeep
  • 23,246
  • 2
  • 28
  • 103