I'm trying to reformat some text by removing newline and duplicated space characters.
My input text looks like this:
hello ! hello
you!
hello
world!!! hello
universe !
and I'm trying to format it like this:
hello !
hello you!
hello world!
hello universe !
I tried using this command:
awk -v RS='!' '{gsub("^ *|\n", ""); gsub(" +", " ")} NF{print $0 RS }' file
But I still get some spaces at the beginning of the line:
hello !
hello you!
hello world!
hello universe !
I don't understand why the first gsub
is not removing the leading space (that should be matched by the pattern ^ *
).
What is wrong is this awk
script?
I'm also interested in the sed
command performing the same formatting.