replacing the `'` char using awk

后端 未结 7 879
梦毁少年i
梦毁少年i 2020-12-14 08:28

I have lines with a single : and a\' in them that I want to get rid of. I want to use awk for this. I\'ve tried using:



        
相关标签:
7条回答
  • 2020-12-14 09:03

    You could use:

    1. Octal code for the single quote:

      [:\47]
      
    2. The single quote inside double quotes, but in that case special characters will be expanded by the shell:

      % print a\': | awk "sub(/[:']/, x)"        
      a
      
    3. Use a dynamic regexp, but there are performance implications related to this approach:

      % print a\': | awk -vrx="[:\\\']" 'sub(rx, x)'  
      a
      
    0 讨论(0)
  • 2020-12-14 09:08

    With bash you cannot insert a single quote inside a literal surrounded with single quotes. Use '"'"' for example.

    First ' closes the current literal, then "'" concatenates it with a literal containing only a single quote, and ' reopens a string literal, which will be also concatenated.

    What you want is:

    awk '{gsub ( "[:'"'"']","" ) ; print $0; }'
    

    ssapkota's alternative is also good ('\'').

    0 讨论(0)
  • 2020-12-14 09:10

    This works:

    awk '{gsub( "[:'\'']","" ); print}'
    
    0 讨论(0)
  • 2020-12-14 09:13

    This also works:

    awk '{gsub("\x27",""); print}'

    0 讨论(0)
  • 2020-12-14 09:24

    I don't know why you are restricting yourself to using awk, anyways you've got many answers from other users. You can also use sed to get rid of " :' "

    sed 's/:\'//g'
    

    This will also serve your purpose. Simple and less complex.

    0 讨论(0)
  • 2020-12-14 09:24

    simplest awk '{gsub(/\047|:/,"")};1'

    0 讨论(0)
提交回复
热议问题