Using sed to replace a number greater than a specified number at a specified position

后端 未结 4 758
失恋的感觉
失恋的感觉 2021-01-25 06:45

I need to write a script to replace all the numbers greater than an specified number which is in following position.

1499011200 310961583 142550756 313415036 14         


        
4条回答
  •  情深已故
    2021-01-25 07:27

    This is doable but not simple. (≥ a number ending is 0's is easier than >.)

    Let's start with a smaller number.

    How could we match numbers greater than 30?

    • 2-digit numbers greater than 30 but less than 40,

      \b3[1-9]\b
      
    • 2-digit numbers 40 or greater,

      \b[4-9][0-9]\b
      
    • numbers with more digits are greater too.

      \b[1-9][0-9]\{2,\}\b
      

    Use alternation to match all the cases.

    \b\(3[1-9]\|[4-9][0-9]\|[0-9]\{3,\}\)\b
    

    300000000 is similar, but more work. Here I've added spaces for readability, but you'll need to remove them in the sed regex.

    \b \( 30000000[1-9]
       \| 3000000[1-9][0-9]
       \| 300000[1-9][0-9]\{2\}
       \| 30000[1-9][0-9]\{3\}
       \| 3000[1-9][0-9]\{4\}
       \| 300[1-9][0-9]\{5\}
       \| 30[1-9][0-9]\{6\}
       \| 3[1-9][0-9]\{7\}
       \| [4-9][0-9]\{8\}
       \| [1-9][0-9]\{9\}
    \) \b
    

提交回复
热议问题