awk gsub question
1
0
Entering edit mode
4.6 years ago
sor.draga • 0

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!

awk • 1.1k views
ADD COMMENT
0
Entering edit mode
cat in.txt  | sed 's/A/@@@/g' | sed 's/B/###/g' | sed 's/C/%%%/g'  > out.txt

and as @ATpoint mentioned:

cat in.txt | awk '{gsub("A","@@@");gsub("B","###");gsub("C","%%%");print}' > out.txt
ADD REPLY
2
Entering edit mode
4.6 years ago
ATpoint 85k

Simplest with tr:

tr "[ABC]" "[@#%]" < your.file > new.file

Or awk:

awk '{gsub("A","@");gsub("B","#");gsub("C","%");print}' your.file > new.file
ADD COMMENT

Login before adding your answer.

Traffic: 2258 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6