开发者

Replacing two strings using awk

开发者 https://www.devze.com 2023-03-01 17:27 出处:网络
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 ther

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开发者_如何学Go }'


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


Just call gsub() twice before printing.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消