sd icon indicating copy to clipboard operation
sd copied to clipboard

How to replace spaces with linebreaks?

Open homocomputeris opened this issue 3 years ago • 2 comments

Which syntax should I use to go from

First word. Another word.

to

First word.
Another word.

sd -s ". " '.\\n' text.txt produces

First word.\n Another word.

with the space intact and no linebreak, just \n.

homocomputeris avatar Oct 20 '20 12:10 homocomputeris

Seems like using -s option makes the replacement string to be literal string as well. You can either use a literal newline or use shell constructs like $'' quoting in bash shell (see https://mywiki.wooledge.org/Quotes for details)

# without -s option
$ echo 'First word. Another word.' | sd '\. ' '.\n'
First word.
Another word.

# with -s option and bash $'' quoting
$ echo 'First word. Another word.' | sd -s '. ' $'.\n'
First word.
Another word.

# -s option and literal newline (press Enter key after .)
$ echo 'First word. Another word.' | sd -s '. ' '.
'
First word.
Another word.

learnbyexample avatar Nov 30 '20 03:11 learnbyexample

I think the issue has to be closed, it is something related to bash and the understanding of -s option

MuhammadSawalhy avatar Aug 11 '21 13:08 MuhammadSawalhy

I've tweaked the documentation on --string-mode to hopefully make it more clear that both the FIND and REPLACE args are treated as literal strings

CosmicHorrorDev avatar May 17 '23 01:05 CosmicHorrorDev