【问题标题】:I want to change the text with sed editor我想用 sed 编辑器更改文本
【发布时间】:2013-12-23 13:15:47
【问题描述】:

如何用 Linux shell 替换文本 (sed, awk, ...)

输入:

aaa decimal(6,3) null,
bbb decimal(12,3) not null,
ccc decimal(1,11) null,
ddd decimal(2,10) null,
ff  decimal(10,1) null,

我想用decimal替换decimal(X,Y)

aaa decimal null,
bbb decimal not null,
ccc decimal null,
ddd decimal null,
ff  decimal null,

【问题讨论】:

    标签: linux shell sed awk


    【解决方案1】:

    试试这个:

    sed 's/(.*)//' infile.txt
    

    【讨论】:

    • 你的答案正在运行,但输入字符(10),输出字符(我只想删除/替换小数(x,y))
    • sed 's/decimal([^)]*)/decimal/' infile.txt
    • 您假设所有行具有相同的格式。 `aaa(4,3) decimal(5,6) null 怎么样,
    【解决方案2】:

    你可以说:

    sed -r 's/(decimal)\([^)]*\)/\1/' filename
    

    对于您的输入,它会产生:

    aaa decimal null,
    bbb decimal not null,
    ccc decimal null,
    ddd decimal null,
    ff  decimal null,
    

    【讨论】:

      【解决方案3】:

      如果每一行都相等并且包含decimal,你可以这样做:

      awk '$2="decimal"' file
      aaa decimal null,
      bbb decimal not null,
      ccc decimal null,
      ddd decimal null,
      ff decimal null,
      

      【讨论】:

        【解决方案4】:
        perl -pi -e 's/decimal\(.*?\)/decimal/g' your_file
        

        【讨论】:

          【解决方案5】:

          使用 awk

          awk '/decimal/{gsub(/\(.*\)/,"")}1' file
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-08-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-19
            • 1970-01-01
            相关资源
            最近更新 更多