AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1195524
Accepted
chess_freak
chess_freak
Asked: 2019-12-12 13:00:38 +0800 CST2019-12-12 13:00:38 +0800 CST 2019-12-12 13:00:38 +0800 CST

如何操作特定字段?

  • 772

我有一个 csv 文件。它的行号最大为 25。

标题是时间(UTC)(第 1 场)、纬度(第 2 场)、经度(第 3 场)、深度(第 4 场)、mag(第 5 场)、地点(第 14 场)等

样本数据

2019-12-10T21:58:28.816Z 35.488 26.4157 57.32 5.4 35km NNE of Palaikastron, Greece


2019-12-11T11:54:27.670Z 18.6158 -67.2838 85 2.85 23km NW of San Antonio, Puerto Rico

首先,我要插入标题,包括内置变量 fieldwith。其次,我想将time(utc)转换为utc+03:00并分为标题的日期和时间并更改其日期格式。第三,我想在第 14 个字段的国家名称之前提取 ' of ' 单词和逗号之间的匹配项。

所需的输出为标题:

Date Time Latitude Longitude Depth Mag Place

期望的输出:

11.12.2019 00:58:28 35.488 26.4157 57.32 5.4 Palaikastron


11.12.2019 14:54:27 18.6158 -67.2838 85 2.85 San Antonio
time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net,id,updated,place,type,horizontalError,depthError,magError,magNst,status,locationSource,magSource
2019-12-06T13:04:46.931Z,-15.2838,-175.1193,10,6,mww,,50,3.512,0.81,us,us60006n19,2019-12-07T13:11:48.228Z,"164km WNW of Hihifo, Tonga",earthquake,8.4,1.9,0.08,15,reviewed,us,us
2019-12-04T20:10:03.614Z,-19.0515,169.5628,266,6,mww,,21,2.812,0.82,us,us60006m2j,2019-12-05T23:44:01.300Z,"63km NNE of Isangel, Vanuatu",earthquake,7.6,1.9,0.037,71,reviewed,us,us
2019-12-03T08:46:36.374Z,-18.5597,-70.6504,32.44,6,mww,,112,0.31,1.4,us,us70006fh7,2019-12-05T08:07:29.617Z,"37km WSW of Arica, Chile",earthquake,6.2,7.8,0.069,20,reviewed,us,us
2019-12-02T05:01:54.693Z,51.3218,-178.2425,27.33,6,mww,,104,0.862,0.97,us,us70006f6d,2019-12-07T02:09:55.119Z,"60km E of Amatignak Island, Alaska",earthquake,6.7,4.2,0.066,22,reviewed,us,us
2019-11-27T07:23:42.552Z,35.7272,23.2673,71.76,6,mww,,23,1.394,1.16,us,us70006dlt,2019-12-03T23:18:27.456Z,"41km NW of Platanos, Greece",earthquake,5.8,5.4,0.046,46,reviewed,us,us
2019-11-26T02:54:12.594Z,41.5112,19.5151,20,6.4,mww,,17,0.937,0.58,us,us70006d0m,2019-12-09T15:46:11.689Z,"16km WSW of Mamurras, Albania",earthquake,3.5,1.8,0.037,72,reviewed,us,us
2019-11-24T00:54:01.052Z,51.3809,-175.5108,20,6.3,mww,,22,0.658,0.95,us,us70006cb6,2019-12-10T01:04:03.731Z,"96km SE of Adak, Alaska",earthquake,3.9,1.8,0.05,38,reviewed,us,us
2019-11-23T12:11:16.261Z,1.6286,132.7854,10,6.1,mww,,38,4.549,1.1,us,us70006c6w,2019-11-25T21:00:33.040Z,"Papua region, Indonesia",earthquake,7.8,1.8,0.061,26,reviewed,us,us
2019-11-20T23:50:43.955Z,19.4533,101.3558,10,6.2,mww,,15,2.366,0.62,us,us70006ara,2019-12-04T05:52:37.313Z,"32km ESE of Chaloem Phra Kiat, Thailand",earthquake,6.4,1.7,0.049,40,reviewed,us,us

如果我能成功一件事,我就不会做其他事。这对我来说是挑战。 请指导我。一方面,难以理解awk为它使用。另一方面,awk时间函数非常有用。我现在很困惑。无论我尝试过什么,我都不擅长。

command-line bash date awk
  • 2 2 个回答
  • 185 Views

2 个回答

  • Voted
  1. Arcege
    2019-12-12T19:24:03+08:002019-12-12T19:24:03+08:00

    首先,您需要预处理 CSV 输入以更好地处理嵌入的逗号。然后将 AWK 分解为功能块。

    $ cat preprocess.sed
    #!/bin/sed -f
    :start   # loop back to here
    /"/{  # for any line that has a double quote
      h   # copy to the hold buffer
      s/[^"]*"\([^"]*\).*/\1/  # what is between the first pair of dquotes
      s/,/@@/g    # replace comma with '@@'
      G   # append the hold buffer to the pattern buffer
          # so we get what was in dqoutes followed by a newline followed by the
          # original line
      s/\(.*\)\n\([^"]*\)"\([^"]*\)"\(.*\)/\2\1\4/
          # replace the unquoted part with what was there
      t start   # go back to 'start'
    }
    

    这将替换".*,.*"with .*@@.*,这将使 AWK 更容易。

    要将日期更改为新时区,请替换第一行:

    $ cat change_date.sh
    #!/bin/sh
    userTZ="${1:-UTC+3}"
    sed 's/,/ /' |
        while read datestr rest; do
            if [ "${datestr}" = time ]; then
                newdate="${datestr}"
            else
                newdate=$(TZ=${userTZ} date -d "${datestr}" "+%d %m %Y %H:%M:%S")
            fi
            echo "${newdate}:${rest}"
    
        done
    

    AWK 脚本如下所示:

    $ cat reformat.awk
    #!/bin/awk  -f
    BEGIN {IFS=","}  # comma separated fields
    NR==1 {print; next;}  # print the header and do nothing more with it
    {   # get just the "town" from the place field
        sub(/.* of /,"",$14)  # strip up to the " of "
        sub(/@@ .*/,"",$14)   # strip after the embedded comma (now '@@')
    }
    {
        printf("%s %8.3f %8.3f %8.3fs %8.3f %s\n", $1, $2, $3, $4, $5, $14)
    }
    

    确保两者都是可执行并运行preprocess.sed sample.csv | change_date.sh | reformat.awk

    或者在一行:

    sed ':start;/"/{;h;s/[^"]*"\([^"]*\).*/\1/;s/,/@@/g;G;s/\(.*\)\n\([^"]*\)"\([^"]*\)"\(.*\)/\2\1\4/;t start;};s/,/ /' test.csv | while read datestr rest; do if [ "$datestr" = "time" ]; then newdate="${datestr}"; else newdate=$(TZ=UTC+3 date -d "$datestr" "+%d %m %Y %H:%M:%S"); fi; echo "${newdate},${rest}"; done | awk -F, 'NR==1 {print;next} {sub(/.* of /,"",$14);sub(/@@ .*/,"",$14)} {printf("%s %8.3f %8.3f %8.3fs %8.3f %s\n", $1, $2, $3, $4, $5, $14)}'
    
    • 2
  2. Best Answer
    steeldriver
    2019-12-13T04:55:35+08:002019-12-13T04:55:35+08:00

    尽管学习 awk 是一个令人钦佩的目标,但它没有用于解析真正的 CSV 文件(特别是可能包含转义或引用分隔符的字段)的内置机制 - 并且时间函数是 GNU 特定的且不可移植。

    由于这些原因,您可能需要考虑使用 Perl(及其Text::CSV模块)、Python - 或者我目前最喜欢的这类东西,Miller。除了提供真正的 CSV 解析外,它们还提供了适当的strptime功能,而即使使用 GNU awk,mktime您也需要手动解析和组合datespec参数。

    例如,在 Miller 中,您可以执行以下操作:

    mlr --csv \
      put -S '
        s = strptime($time,"%Y-%m-%dT%H:%M:%SZ") + 3*3600; 
        $date = strftime(s,"%d.%m.%Y"); 
        $time = strftime(s,"%H:%M:%S"); 
        $place =~ "(.* of |)([^,]*),(.*)$" { $place = "\2" }
      ' then cut -o -f date,time,latitude,longitude,depth,mag,place input.csv
    

    如果您想要空格分隔的输出列,请更改--csv为--icsv --opprint(“漂亮打印的”表格输出 - 带有标题)或--icsv --onidx(简单的空格分隔输出)。

    前任。

    $ mlr --icsv --opprint   put -S '
        s = strptime($time,"%Y-%m-%dT%H:%M:%SZ") + 3*3600; 
        $date = strftime(s,"%d.%m.%Y"); 
        $time = strftime(s,"%H:%M:%S"); 
        $place =~ "(.* of |)([^,]*),(.*)$" { $place = "\2" }
      ' then cut -o -f date,time,latitude,longitude,depth,mag,place input.csv
    date       time     latitude longitude depth mag place
    06.12.2019 16:04:46 -15.2838 -175.1193 10    6   Hihifo
    04.12.2019 23:10:03 -19.0515 169.5628  266   6   Isangel
    03.12.2019 11:46:36 -18.5597 -70.6504  32.44 6   Arica
    02.12.2019 08:01:54 51.3218  -178.2425 27.33 6   Amatignak Island
    27.11.2019 10:23:42 35.7272  23.2673   71.76 6   Platanos
    26.11.2019 05:54:12 41.5112  19.5151   20    6.4 Mamurras
    24.11.2019 03:54:01 51.3809  -175.5108 20    6.3 Adak
    23.11.2019 15:11:16 1.6286   132.7854  10    6.1 Papua region
    21.11.2019 02:50:43 19.4533  101.3558  10    6.2 Chaloem Phra Kiat
    

    Miller 可从 Ubuntuuniverse存储库获得。

    • 2

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve