转换科学记数法在多个领域的十进制 [英] Convert scientific notation to decimal in multiple fields

查看:117
本文介绍了转换科学记数法在多个领域的十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的第12列数据极大以下的作品,但我有超​​过70列是各不相同的,我需要输出的所有列,转换后的那些取代科学价值。

 的awk -F',''{printf的%.41f \\ n,$ 12}'$文件

感谢

这是一条线。

<$p$p><$c$c>2012-07-01T21:59:50,2012-07-01T21:59:00,1817,22901,264,283,549,1,2012-06-24T13:20:00,2.600000000000000e+001,4.152327506554059e+001,-7.893523806678388e+001,5.447572631835938e+002,2.093000000000000e+003,5.295000000000000e+003,1,194733,1.647400093078613e+001,31047680,1152540,29895140,4738,1.586914062500000e+000,-1.150000000000000e+002,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,3.606000000000000e+003,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,4.557073364257813e+002,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,11,0.000000000000000e+000,2.000000000000000e+000,0,0,0,0,4.466836981009692e-004,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,8,0,840,1,600,1,6,1,1,1,5,2,2,2,1,1,1,1,4854347,0,-

更新

这是对非转换输出工作。我有一点麻烦if语句插入别人出于某种原因。一切似乎给我一个语法错误在一个文件或CLI。

 的awk -F',''{为(i = 1; I&LT; = NF;我++){如果(I&LT; = 9 ||我== 16 ||我== || 17 ==我19 ||我== || 20 ==我21 ||我== || 22 ==我40 ||我== || 43 ==我44 ||我== 45 ||我== || 46 I&GT; = 51)的printf($ I,)};}'$文件

我想插入下面​​的语句到code以上??

 否则,如果(我== 10)的printf(%.41f,$ I)

解决

得到它的工作。感谢所有伟大的想法。我似乎无法使它在使用awk -f,但在命令行上这是伟大的工作文件工作。我把这个单行在我的计划。

 的awk -F',''{为(i = 1; I&LT; = NF;我++){如果(I&LT; = 9 ||我== 16 ||我== 17 || I&GT; = 19安培;&安培; I&LT; = 22 ||我== 40 || I&GT; = 43&放大器;&安培; I&LT; = 46 || I&GT; = 51&放大器;&安培;我&LT; = 70)的printf($ I,);否则,如果(我== || 10 ==我18)的printf(,$ I%2F。);否则,如果(我== || 11 ==我12)的printf(,$ I%41F。);否则,如果(我== 13)的printf(%1楼。$ I);否则,如果(我== || 14 ==我15 || I&GT; = 24安培;&安培; I&LT; = 46)的printf(%d个,$ I);否则,如果(我== 23)的printf(%4F。$ I);否则,如果(I&GT; = 47安培;&安培; I&LT; = 50)的printf(%1207米。$ I);如果(我== 71)的printf($ I\\ n)};}'

结果

<$p$p><$c$c>2012-07-01T21:59:50,2012-07-01T21:59:00,1817,22901,264,283,549,1,2012-06-24T13:20:00,26.00,41.52327506554058800247730687260627746582031,-78.93523806678388154978165403008460998535156,544.8,2093,5295,1,194733,16.47,31047680,1152540,29895140,4738,1.5869,-115,0,0,0,0,0,0,0,3606,0,0,0,455,0,0,0,11,0,2,0,0,0,0,0.000447,0.000000,0.000000,0.000000,8,0,840,1,600,1,6,1,1,1,5,2,2,2,1,1,1,1,4854347,0,-


解决方案

您可以做正则表达式匹配在循环中选择每个字段的格式,因为数字也串在AWK:

 #!的/ usr /斌/的awk -f
开始 {
    D =[[:数字:]]
    OFS = FS =,
}
{
    DELIM =
    对于(i = 1; I&LT; = NF;我++){
        如果($ I〜DE +D D D$){
            printf的%s%.41f,DELIM,$ I
        }
        其他{
            printf的%s%S,DELIM,$ I
        }
        DELIM = OFS
    }
    printf的\\ n
}

编辑:

我已经改变了上面的版本,所以你可以看到它是如何将一个文件作为AWK脚本中使用。省省吧(我称之为scinote),并将其设置为可执行 CHMODü+ X scinote ,那么你可以像这样运行: ./scinote inputfile中

我也修改您添加到你的问题的最新版本,使之成为简单一些,因此它已经准备好进入一个脚本文件如上面。

 #!的/ usr /斌/的awk -f
开始 {
    plainlist =16 17 19 20 21 22 40 43 44 45 46
    拆分(plainlist,ARR)
    为(i的改编){
        plainfmt [ARR [I] =%S
    }
    OFS = FS =,
}
{
    DELIM =
    对于(i = 1; I&LT; = NF;我++){
        printf的%S,DELIM
        如果(I&LT; = 9 ||我在plainfmt || I&GT; = 51){
            printf的plainfmt [I],$ I
        }
        否则,如果(我== 10){
            printf的%.41f,$ I
        }
        否则,如果(我== 12){
            printf的%.12f,$ I
        }
        DELIM = OFS
    }
    printf的\\ n
}

如果您有其他格式(而不是一个每次),你可以做类似的东西更多的领域plainfmt 阵列。

The following works great on my data in column 12 but I have over 70 columns that are not all the same and I need to output all of the columns, the converted ones replacing the scientific values.

 awk -F',' '{printf "%.41f\n", $12}' $file

Thanks

This is one line..

2012-07-01T21:59:50,2012-07-01T21:59:00,1817,22901,264,283,549,1,2012-06-24T13:20:00,2.600000000000000e+001,4.152327506554059e+001,-7.893523806678388e+001,5.447572631835938e+002,2.093000000000000e+003,5.295000000000000e+003,1,194733,1.647400093078613e+001,31047680,1152540,29895140,4738,1.586914062500000e+000,-1.150000000000000e+002,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,3.606000000000000e+003,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,4.557073364257813e+002,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,11,0.000000000000000e+000,2.000000000000000e+000,0,0,0,0,4.466836981009692e-004,0.000000000000000e+000,0.000000000000000e+000,0.000000000000000e+000,8,0,840,1,600,1,6,1,1,1,5,2,2,2,1,1,1,1,4854347,0,-

UPDATE

This is working for the non converted output. I am having a bit of trouble inserting an else if statement for some reason. Everything seems to give me a syntax error in a file or on cli.

awk -F',' '{for (i=1;i<=NF;i++) {if (i <= 9||i == 16||i == 17||i == 19||i == 20||i == 21||i == 22|| i == 40|| i == 43||i == 44||i == 45||i == 46||i >= 51) printf ($i",")};}' $file

I would like to insert the following statement into the code above??

else if (i == 10) printf ("%.41f", $i)

SOLVED

Got it worked out. Thanks for all the great ideas. I can't seem to make it work in a file with awk -f but on the command line this is working great. I put this one liner in my program.

awk -F',' '{for (i=1;i<=NF;i++) {if (i <= 9||i == 16||i == 17||i >= 19&&i <= 22|| i == 40|| i >= 43&&i <= 46||i >= 51&&i <= 70) printf($i","); else if (i == 10||i == 18) printf("%.2f,", $i); else if (i == 11||i == 12) printf("%.41f,", $i); else if (i == 13) printf("%.1f,", $i); else if (i == 14||i == 15||i >= 24&&i <= 46) printf ("%d,", $i); else if (i == 23) printf("%.4f,", $i); else if (i >= 47&&i <= 50) printf("%.6f,", $i); if (i == 71) printf ($i"\n")};}'

RESULT

2012-07-01T21:59:50,2012-07-01T21:59:00,1817,22901,264,283,549,1,2012-06-24T13:20:00,26.00,41.52327506554058800247730687260627746582031,-78.93523806678388154978165403008460998535156,544.8,2093,5295,1,194733,16.47,31047680,1152540,29895140,4738,1.5869,-115,0,0,0,0,0,0,0,3606,0,0,0,455,0,0,0,11,0,2,0,0,0,0,0.000447,0.000000,0.000000,0.000000,8,0,840,1,600,1,6,1,1,1,5,2,2,2,1,1,1,1,4854347,0,-

解决方案

You can do regex matching in a loop to choose the format for each field since numbers are also strings in AWK:

#!/usr/bin/awk -f
BEGIN {
    d = "[[:digit:]]"
    OFS = FS = ","
}
{
    delim = ""
    for (i = 1; i <= NF; i++) {
        if ($i ~ d "e+" d d d "$") {
            printf "%s%.41f", delim, $i
        }
        else {
            printf "%s%s", delim, $i
        }
        delim = OFS
    }
    printf "\n"
}

Edit:

I've changed the version above so you can see how it would be used in a file as an AWK script. Save it (I'll call it "scinote") and set it as executable chmod u+x scinote, then you can run it like this: ./scinote inputfile

I've also modified the latest version you added to your question to make it a little simpler and so it's ready to go into a script file as above.

#!/usr/bin/awk -f
BEGIN {
    plainlist = "16 17 19 20 21 22 40 43 44 45 46"
    split(plainlist, arr)
    for (i in arr) {
        plainfmt[arr[i]] = "%s"
    }
    OFS = FS = ","
}
{
    delim = ""
    for (i = 1; i <= NF; i++) {
        printf "%s", delim
        if (i <= 9 || i in plainfmt || i >= 51) {
            printf plainfmt[i], $i
        }
        else if (i == 10) {
            printf "%.41f", $i
        }
        else if (i == 12) {
            printf "%.12f", $i
        }
        delim = OFS
    }
    printf "\n"
}

If you had more fields with other formats (rather than just one per), you could do something similar to the plainfmt array.

这篇关于转换科学记数法在多个领域的十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆