6

I want to replace @@ with ^ and ¤¤ with a newline in a file. To do this I wrote the code below, but it feels like there is a more elegant solution then calling gawk twice. Can anyone tell me if there is one?

cat test.txt | gawk '{ gsub("@@", "^"); print }' | gawk '{ gsub("¤¤", "\r\n"); print }'

2 Answers 2

14

First, skin away the cat. Its useless except for file concatenation, which is its purpose. your awk command would be

awk '{gsub("@@","^");gsub("¤¤","\r\n");print}' file

If you want to remove all line breaks before doing the above

tr -d '\r\n' <file > temp && mv temp file
Sign up to request clarification or add additional context in comments.

1 Comment

What if I want to also remove all line breaks BEFORE doing all of the substitutions above?
2

Just call gsub() twice before printing.

gawk '{ gsub("@@", "^"); gsub("¤¤", "\r\n"); print }'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.