1

Using bash command syntax:

echo "The*quick*brown*fox"|sed s/*/\\n/g

I get the desired output:

The
quick
brown
fox

However in script:

IN_TXT="The*quick*brown*fox"
OUT_TXT=$(echo $IN_TXT|sed s/*/\\n/g)
echo $OUT_TXT

I get whitespaced: The quick brown fox

What am I doing wrong?

Kusalananda
  • 333,661
SPB
  • 13

1 Answers1

2

You're not quoting the variable OUT_TXT when you use it with echo. This causes the shell to split the variable's value on spaces, tabs and newlines into words that are then given as arguments to echo (after additionally undergoing filename globbing expansion). echo then outputs those arguments with spaces in-between them.

Quote you variable expansions, always:

echo "$OUT_TXT"

Also note that you have an issue in echo $IN_TXT. If the expansion of the IN_TXT variable isn't quoted as "$IN_TXT", its value would be used as a globbing pattern and would be replaced by any matching filename (test this with IN_TXT='*', for example).

A similar issue is present in your sed expression, since it's not properly quoted.

See also:

Also, your sed command is better wirtten as sed 'y/*/\n/' as standard sed simply can't insert newlines with the s/// command (GNU sed is an exception). The sed command y/// does know how to change single characters into newlines, regardless of what implementation of sed is used.

Or, you could simply use the tr command as tr '*' '\n', or a quick variable substitution in the shell:

printf '%s\n' "${IN_TXT//\*/ }"

or,

OUT_TXT=${IN_TXT//\*/ }
printf '%s\n' "$OUT_TXT"
Kusalananda
  • 333,661