awk gsub question
Hello, guys!
I've used awk in the past for large file manipulation and substitutions.Recently, I've used it to substitute, for ex letter A with a set of characters:
$ awk '{gsub(/A/,"@@@")}1' in.txt >> out.txt
where in.txt contains strings of letters of various length. (AAA, BBB, CCC, ABABAB etc)
How can I use gsub to replace all characters A in my file with @@@, B with ###, C with %%% etc
I am guessing it should be something close to:
$ awk '{gsub(/A|B|C/,"&123")}1' in.txt > out.txt
Many thanks!
• 1,506 views
•
link
1 answer
Simplest with tr:
tr "[ABC]" "[@#%]" < your.file > new.file
Or awk:
awk '{gsub("A","@");gsub("B","#");gsub("C","%");print}' your.file > new.file
• 0 views
•
link
Log in to answer this question.
and as @ATpoint mentioned: