【问题标题】:Print a comma every two fields in bash在bash中每两个字段打印一个逗号
【发布时间】:2014-11-12 14:31:19
【问题描述】:

例如,我有一个姓名列表(名字和姓氏):

Tom Smith Mary Brown Harry Anderson Sally Hall

我想将其输出为:

Tom Smith, Mary Brown, Harry Anderson, Sally Hall

如果我/您可以在“最后一个”逗号后添加 'and',则可获得奖励积分。

现在我知道我可以将它放入 for 循环中并打印和迭代并检查是否是从头到尾的三个字段,但我认为使用 awk 或其他命令必须有更简单的方法。

有什么想法吗?

【问题讨论】:

  • 如果您不知道,您之所以会吸引如此多的反对票,是因为您没有尝试自己解决这个问题。
  • 我的尝试会很混乱。我确实提到过我会把它扔到一个 for 循环中,一次打印两个并迭代。我知道这很混乱,也知道我来论坛的原因。但是你我想知道。
  • 也许它会很混乱,但展示你解决问题的尝试总是比简单地发布问题更好。将来,您应该这样做。

标签: bash loops printing awk


【解决方案1】:

只需使用 sed 即可:

$ sed -r 's/(\w* \w*) /\1, /g' <<< "Tom Smith Mary Brown Harry Anderson Sally Hall"
Tom Smith, Mary Brown, Harry Anderson, Sally Hall

这会选择一组由两个空格分隔的单词并用逗号将其打印回来。

要打印and 而不是最后一个逗号,请捕获最后一个逗号之前的所有内容,然后用文本and 打印回来:

$ sed -r -e 's/(\w* \w*) /\1, /g' -e 's/, ([^,]*)$/ and \1/' <<< "Tom Smith Mary Brown Harry Anderson Sally Hall" 
Tom Smith, Mary Brown, Harry Anderson and Sally Hall

【讨论】:

  • 我上次和你一起去了。除了多个名称组合外,它还考虑单个名称组合。谢谢!
【解决方案2】:
$ awk '{for (i=1;i<(NF-2);i+=2) printf "%s %s, ", $i, $(i+1); print "and", $(NF-1), $NF}' file
Tom Smith, Mary Brown, Harry Anderson, and Sally Hall

【讨论】:

    【解决方案3】:

    我想我会添加另一个 awk

    awk '{while(++i<NF-2)printf "%s",$i (i%2?FS:", ");print $i" and "$++i,$NF}'
    
    Tom Smith, Mary Brown, Harry Anderson and Sally Hall
    

    如果你想在 and 前加逗号

    awk '{while(++i<NF-1)printf "%s",$i (i%2?FS:", ");print "and "$i,$NF}'
    Tom Smith, Mary Brown, Harry Anderson,and Sally Hall
    

    没有和

     awk '{while(++i<NF)printf "%s",$i (i%2?FS:", ");print $NF}'
     Tom Smith, Mary Brown, Harry Anderson, Sally Hall
    

    最后是我的和anubhavas的混合体

     awk '{while((i+=2)<NF-1)$i=$i","}--i&&$i="and "$i'
    

    【讨论】:

    • 我稍微修改了一下,但是换个角度看还是不错的。谢谢。 awk '{while(++i
    【解决方案4】:

    使用 awk:

    s='Tom Smith Mary Brown Harry Anderson Sally Hall'
    awk '{for (i=2; i<NF-1; i+=2) $i = $i ","; $(NF-1)="and " $(NF-1)} 1' <<< "$s"
    Tom Smith, Mary Brown, Harry Anderson, and Sally Hall
    

    【讨论】:

      【解决方案5】:

      使用 awk

      echo "Tom Smith Mary Brown Harry Anderson Sally Hall
      1 2 3 4" | awk '{for (i=1;i<NF-2;i+=2){printf("%s %s, ", $i, $(i+1)) };print "and "$(NF-1), $NF;}'
      
      Tom Smith, Mary Brown, Harry Anderson, and Sally Hall
      1 2, and 3 4
      

      【讨论】:

        【解决方案6】:

        一个相当简单的,使用set

        set -- Tom Smith Mary Brown Harry Anderson Sally Hall
        while (($#)); do
            printf '%s %s' "$1" "$2"
            shift 2 || shift
            if (($#>2)); then printf ', '; elif (($#)); then printf ' and '; fi
        done
        printf '\n'
        

        这样您就可以将所有这些都放在一个不错的函数中:

        sep_them() {
            while (($#)); do
                printf '%s %s' "$1" "$2"
                shift 2 || shift
                if (($#>2)); then printf ', '; elif (($#)); then printf ' and '; fi
            done
            printf '\n'
        }
        

        并使用它:

        gniourf$ sep_them one
        one 
        gniourf$ sep_them one two
        one two
        gniourf$ sep_them one two three
        one two and three 
        gniourf$ sep_them one two three four
        one two and three four
        gniourf$ sep_them one two three four five
        one two, three four and five 
        gniourf$ sep_them one two three four five six
        one two, three four and five six
        gniourf$ sep_them one two three four five six seven
        one two, three four, five six and seven 
        gniourf$ sep_them one two three four five six seven eight
        one two, three four, five six and seven eight
        gniourf$ sep_them one two three four five six seven "eight with a space"
        one two, three four, five six and seven eight with a space
        

        【讨论】:

          猜你喜欢
          • 2010-12-31
          • 1970-01-01
          • 1970-01-01
          • 2018-12-28
          • 2018-06-03
          • 1970-01-01
          • 2017-07-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多