【问题标题】:How to replace a particular length of characters with same length of unique digits如何用相同长度的唯一数字替换特定长度的字符
【发布时间】:2017-07-23 09:47:53
【问题描述】:

我想使用 UNIX 命令将文件 file.txt 中的 16 位数字替换为 1111111111111111,即 16 乘以 1

【问题讨论】:

  • 您为解决这个问题做了哪些尝试?
  • I want to replace 16 digits in a file file.txt with 1111111111111111 i.e. 16 times 1 using UNIX command 当然!没问题!

标签: bash shell unix


【解决方案1】:

试试这个 -

sed -E 's/[0-9]{16}/1111111111111111/g' f

【讨论】:

    【解决方案2】:

    Perl:

    perl -plE 's/\b\d{16}\b/q{1}x16/ge' < file.txt
    

    来自

    line A 222222222222222   and 222222222222222    # fifteen
    line B 3333333333333333  and 3333333333333333   # sixteen
    line C 66666666666666666 and 66666666666666666  # seventeen
    

    生产

    line A 222222222222222   and 222222222222222    # fifteen
    line B 1111111111111111  and 1111111111111111   # sixteen
    line C 66666666666666666 and 66666666666666666  # seventeen
    

    还有

    perl -plE 's/\b(\d+)\b/q{1} x length($1)/ge'
    

    从相同的输入会产生

    line A 111111111111111   and 111111111111111    # fifteen
    line B 1111111111111111  and 1111111111111111   # sixteen
    line C 11111111111111111 and 11111111111111111  # seventeen
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      相关资源
      最近更新 更多