Replacing two strings using awk

11,617

Solution 1

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

Solution 2

Just call gsub() twice before printing.

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

Share:
11,617
rickythefox
Author by

rickythefox

Self-employed IT consultant with experience in finance, banking, healthcare and e-tailing sectors. @rickythefox on Twitter

Updated on June 13, 2022

Comments

  • rickythefox
    rickythefox about 2 years

    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 }'
    
  • rickythefox
    rickythefox about 13 years
    What if I want to also remove all line breaks BEFORE doing all of the substitutions above?