【问题标题】:Remove Specific Pattern at the end of the string awk删除字符串 awk 末尾的特定模式
【发布时间】:2017-01-27 06:12:46
【问题描述】:

我有如下字符串,

Abc \ \          <--- This is With space at the end
Abc \ \          <--- This is Without space at the end
Abc\ \
Abc \\
Abc\\
Abc\ 
Abc \ 

以上所有字符串的预期结果,

Abc

简而言之,如果在字符串的末尾有任何空格 & \ 的组合模式,那么我想删除它。 我想用 awk 来做这件事。

请帮忙...

【问题讨论】:

  • 那么,你有什么尝试吗?

标签: regex awk gsub


【解决方案1】:

要对每一行应用一个替换,请使用 sed:

sed 's/[ \]*$//g' file

匹配每行末尾的所有空格和反斜杠组合,并替换为空。

【讨论】:

    【解决方案2】:

    使用 awk 你可以做类似awk '{gsub(/[ \\]+$/,"")}1' file

    测试:

    $ cat -A file
    Abc \ \ $
    Abc \ \$
    Abc\ \$
    Abc \\$
    Abc\\$
    Abc\ $
    Abc \ $
    
    $ awk '{gsub(/[ \\]+$/,"")}1' file
    Abc
    Abc
    Abc
    Abc
    Abc
    Abc
    Abc
    

    正则表达式匹配行尾的连续空格和反斜杠序列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 2011-03-07
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      相关资源
      最近更新 更多