21

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:

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

and

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

and

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

non of them worked, but return the error Unmatched ".. when I put

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

then It works and removes all : and _ chars. How can I get rid of the ' char?

SIMEL
  • 8,745
  • 28
  • 84
  • 130

7 Answers7

13

tr is made for this purpose

echo test\'\'\'\':::string | tr -d \':
teststring

$ echo test\'\'\'\':::string | awk '{gsub(/[:\47]*/,"");print $0}'
teststring
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
10

This works:

awk '{gsub( "[:'\'']","" ); print}'
ssapkota
  • 3,262
  • 19
  • 30
9

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
    
Greg Dubicki
  • 5,983
  • 3
  • 55
  • 68
Dimitre Radoulov
  • 27,252
  • 4
  • 40
  • 48
6

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 ('\'').

Benoit
  • 76,634
  • 23
  • 210
  • 236
4

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.

v3nM
  • 952
  • 1
  • 12
  • 19
3

This also works:

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

Andy Barbour
  • 8,783
  • 1
  • 24
  • 35
  • I've heard that hex characters tend to be very different between various implementations of awk. See [here](http://awk.freeshell.org/Frequently_Asked_Questions#toc14). Better on some implementations to use octal numbers, such as `"\047"`. – TSJNachos117 Oct 30 '19 at 05:22
  • To clarify my previous comment: hex chars can behave differently in certain circumstances. For example `print "\x27foo\x27"` will work with mawk and busybox awk, but not gawk. – TSJNachos117 Oct 30 '19 at 05:37
1

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

Henry Barber
  • 123
  • 1
  • 5