Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing two strings using awk

Tags:

awk

gawk

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 }'
like image 801
rickythefox Avatar asked Apr 29 '11 07:04

rickythefox


1 Answers

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
like image 190
ghostdog74 Avatar answered Oct 02 '22 03:10

ghostdog74