ffej01
ffej01

Reputation: 3

Trying to remove punctuation with AWK

I have a script that I am trying to clean up the ~ from the fields.

awk '{gsub(/[[:punct:]]/, "")} 1' RS='[[:space:]]'/test/data/USER.FIDS
~qqq~
~jeff~
~mark~

This is how I would like to clean this up, and remove the Tilde from the fields.

qqq
jeff
mark

Upvotes: 0

Views: 79

Answers (2)

karakfa
karakfa

Reputation: 67527

even simpler with tr

$ echo "~qqq~ ~jeff~ ~mark~" | tr -d '~'

qqq jeff mark

Upvotes: 2

David C. Rankin
David C. Rankin

Reputation: 84599

Awk is actually a more difficult solution. You actually want sed that makes the operation trivial, e.g.

sed 's/~//g'

Where s/find/replace/ is the normal substitution form with find equal to "~" and replace with nothing (empty-string). The g makes the substitutions global (all occurrences).

Example Use/Output

$ echo "~qqq~ ~jeff~ ~mark~" | sed 's/~//g'
qqq jeff mark

Of course the awk solution is equally short, e.g.

awk '{gsub(/~/,"")}1'

Which does exactly the same thing using gsub() (global substitute).

Example Use/Output

$ echo "~qqq~ ~jeff~ ~mark~" | awk '{gsub(/~/,"")}1'
qqq jeff mark

Upvotes: 1

Related Questions