【问题标题】:How to format CSV file using awk commands (Trying to avoid manual work around in the csv file) with out doing delimiter in the CSV file如何使用 awk 命令格式化 CSV 文件(试图避免在 csv 文件中手动工作)而不在 CSV 文件中做分隔符
【发布时间】:2020-03-06 05:03:12
【问题描述】:

我是AWK的新手,提前感谢您的建议。

我在这里有一个问题,我得到如下所示的单行而不是多行的输出

hostname port
http://example.com/token                                                       80
https://digits.com                                                                  443
https://examples.demo.com?grant_type            443
http://demo/paying/security/tokens/demoitexample.com                                   80
http://demo/paying/security/tokens/demoitexample1.com                                  80
http://demo/paying/security/tokens/demoitexample2.com                                  80
http://demo/paying/security/tokens/demoitexample2.com                                  80

但我想在不手动更改 csv 文件的情况下获得如下输出

预期输出如下图

hostname,port
http://example.com/token,80
https://digits.com,443
https://examples.demo.com?grant_type,443
http://demo/paying/security/tokens/demoitexample.com,80
http://demo/paying/security/tokens/demoitexample1.com,80
http://demo/paying/security/tokens/demoitexample2.com,80
http://demo/paying/security/tokens/demoitexample2.com,80

这是我得到输出的代码,如果我们可以将两个命令组合在一个命令中,那就太好了。

grep -P '((?<=[^0-9.]|^)[1-9][0-9]{0,2}(\.([0-9]{0,3})){3}(?=[^0-9.]|$)|(http|ftp|https|ftps|sftp)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/+#-]*[\w@?^=%&/+#-])?|\.port|\.host|contact-points|\.uri|\.endpoint)' abc.properties| grep '^[^#]'| awk '{split($0,a,"#"); print a[1]}' | awk '{split($0,a,"="); print a[1],a[2]}'|sed 's/^\|#/,/g'|awk '/http:\/\//  {print $2,80}
       /https:\/\// {print $2,443}
       /Points/     {print $2,"9042"}
       /host/       {h=$2}
       /port/       {print h,$2; h=""}'|awk -F'[, ]' '{for(i=1;i<NF;i++){print $i,$NF}}'| column -t

【问题讨论】:

    标签: bash shell csv awk


    【解决方案1】:

    您是否可以使用您展示的示例尝试以下操作。

    awk 'BEGIN{OFS=","} {$1=$1} 1' Input_file
    

    或者,如果您只有 2 个字段要处理,请按照@Rafaf 评论尝试:

    awk '{print $1","$2}' Input_file
    OR
    awk 'BEGIN{OFS=","} {print $1,$2}' Input_file
    

    【讨论】:

      【解决方案2】:

      RavinderSingh13的解决方法有个坑:如果URL中包含逗号,则输出的CSV会被破坏。根据定义,URL 不能包含空格(或 BLANK),因此空格是此处的保存字段分隔符。

      关键输入记录示例:

      http://demo/paying/security/tokens/demoitexample1.com?a=b,c  80
      http://demo/paying/security/tokens,more/demoitexample2.com   80
      

      一个解决方案是这个 awk 命令:

      awk '{ gsub(/"/,"\\\""); printf("\"%s\",\"%s\"\n",$1,$2)}' Input_file
      

      这里引用了 URL 和 PORT。如果您可以保证它是一个简单的词,则不需要引用端口。此外,所有" 字符(在URL 中无效,但很少见)都被\" 替换。所以 URL 中的 " 不会破坏输出。我从未在 URL 中看到反斜杠,因此我没有将其替换为 gsub()。无论如何,这样做:gsub(/["\\\\]/,"\\\\&amp;");(4x 反斜杠 \\\\ 被 bash 减少为 \\)。

      【讨论】:

      • 感谢您的建议,我也厌倦了您的脚本,您的命令将在 CSV 文件中以逗号分隔输出,稍后我们需要在 CSV 文件中进行操作。
      • 逗号在某些网站上是允许的并且是常用的。它们用于例如如果参数允许数字列表:https://DOMAIN/jobs/?j=copy,clean。并且一些网站支持没有查询部分的漂亮 URL:https://DOMAIN/jobs/copy,clean.
      猜你喜欢
      • 1970-01-01
      • 2022-01-07
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      相关资源
      最近更新 更多