Lvcrft
Lvcrft

Reputation: 57

Replace all non-alphanumeric characters with AWK

I know that using sed -E 's/[^[:alnum:][:space:]]+/?/g $input' will replace all non-alphanumeric characters of my input file with question mark.

How am I supposed to do the same thing with AWK?

Upvotes: 1

Views: 1364

Answers (2)

karakfa
karakfa

Reputation: 67567

why not tr?

  ... | tr -c '[:alnum:][:space:]' '?'

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133770

You need to fix your regex as well as you need to use awk's gsub function for your requirement. since we are mentioning gsub(global substitution) we need not to use + here as it will catch everything at once and will replace everything with a single ?.

awk '{gsub(/[^[:alnum:][:space:]]/,"?")} 1' Input_file

With input Hello!@#$ it will become as Hello????.

Upvotes: 4

Related Questions