Harry Watson
Harry Watson

Reputation: 71

Removing punctuation from txt file in Linux terminal using 'tr' and 'awk' commands

I'm currently taking a crash course in the basics of the Linux terminal and one of the tasks is to replace punctuation in a text file using 'awk' and 'tr' commands. I have tried searching around for solutions but nothing is working for me, any help?

Upvotes: 7

Views: 17883

Answers (2)

Emj899
Emj899

Reputation: 1

tr --help, the -d deletes tr -d [:punct:] < inputfile > outputfile

Upvotes: 0

Andriy Makukha
Andriy Makukha

Reputation: 8324

Using tr (as Glenn Jackman has already pointed out):

cat TEXTFILE | tr -d '[:punct:]' > OUTFILE

Using awk (tested with gawk and mawk):

cat TEXTFILE | awk '{ gsub(/[[:punct:]]/, "", $0) } 1;' > OUTFILE

You can also omit cat with AWK:

awk '{ gsub(/[[:punct:]]/, "", $0) } 1;' TEXTFILE > OUTFILE

Note: TEXTFILE and OUTFILE must be different.

Upvotes: 10

Related Questions