【问题标题】:Move numbers at the beginning of the line to the end of the line将行首的数字移动到行尾
【发布时间】:2018-04-17 10:26:47
【问题描述】:

我有一个来自 Unix uniq -c 命令的输出,该命令在每行的开头打印字符串的出现次数。该字符串表示由管道分隔的两个作者(例如,Aabdel-Wahab S|Abdel-Hafeez EH)。

  1 Aabdel-Wahab S|Abdel-Hafeez EH
  1 Aabdel-Wahab S|Abdulla AM
  4 Aabdel-Wahab S|Ahmad AK
  1 Aabdel-Wahab S|Mosalem FA
  1 Aabye MG|Andersen AB
  8 Aabye MG|Changalucha J
  1 Aabye MG|Christensen DL
  1 Aabye MG|Faurholt-Jepsen D

我需要 grep 出现次数并将其移动到行尾。例如:

Aabdel-Wahab S|Abdel-Hafeez EH|1
Aabdel-Wahab S|Abdulla AM|1
Aabdel-Wahab S|Ahmad AK|4
Aabdel-Wahab S|Mosalem FA|1
Aabye MG|Andersen AB|1
Aabye MG|Changalucha J|8
Aabye MG|Christensen DL|1
Aabye MG|Faurholt-Jepsen D|1

请注意,频率现在用竖线分隔。下面粘贴的是我在 Awk 中的单行代码:

awk '{num=$1;$1=""; sub(/^ /,""); print $0,"|",num;}' file

但是,Awk 在最终管道周围添加了额外的空格:

Aabdel-Wahab S|Abdel-Hafeez EH | 1
Aabdel-Wahab S|Abdulla AM | 1
Aabdel-Wahab S|Ahmad AK | 4
Aabdel-Wahab S|Mosalem FA | 1
Aabye MG|Andersen AB | 1
Aabye MG|Changalucha J | 8
Aabye MG|Christensen DL | 1
Aabye MG|Faurholt-Jepsen D | 1

知道如何继续(不需要使用 Awk)吗?

【问题讨论】:

    标签: awk sed text-processing


    【解决方案1】:

    这是使用sed 而不是awk 的真实案例:

    sed 's/^  *\([0-9][0-9]*\) *\(.*\)/\2|\1/' file
    

    正则表达式分解:

    • ^ * 至少以一个空格开头
    • \( 第一组抓包开始
      • [0-9][0-9]*至少匹配一位数字
    • \)CG一完结
    • * 任意数量的空格字符
    • \(.*\) 捕获输入行的其余部分(CG 两个)

    替换字符串更改捕获组的顺序,其间有一个|

    【讨论】:

    • 同意。 sed 是在这样的单个行上进行简单替换的正确工具。
    【解决方案2】:

    Awk 本身不添加空格,您是在告诉 awk 添加空格。您认为,print 1,2 中的含义是什么(提示:在awk 手册页中查找OFS)?只是不要那样做:

    awk '{num=$1; $1=""; sub(/^ /,""); print $0 "|" num}' file
    

    【讨论】:

      【解决方案3】:

      你可以使用printf:

      awk '{num=$1;$1=""; sub(/^ /,""); printf("%s|%s\n",$0,num);}' file
      

      【讨论】:

        【解决方案4】:

        使用sed

        sed -r 's/\s*([0-9]+)\s*(.*)/\2|\1/' infile
        
        • \s* 匹配零个或多个空格。
        • ([0-9]+) 匹配用于组匹配的一个或多个数字和括号。
        • (.*) 匹配任何内容,并且此处也再次用于组匹配的括号。
        • \2|\1 中,我们正在打印第二组比赛,即(.*),下一个第一组比赛,即([0-9]+),中间有管道。

        POSIXly,你会这样做:

        sed 's/^ *\([0-9][0-9]*\) *\(.*\)$/\2|\1/' infile
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-05
          • 1970-01-01
          相关资源
          最近更新 更多