Skip to content
Advertisement

Bash – one-liner echo with delimiter

So my sample code so far looks like this:

#!/bin/bash

mysampletxt="=======nTestn-------;----------nDone Testn=========="

I want to be able to echo it out to make it look like the following:

=======
Test
-------
Some code goes here
More code
----------
Done Test
==========

But the problem is that when I tried using AWK, sed, or IFS they also use n as delimiters which I don’t want that to happen as the text gets all messed up. Is there anyway I can include ; as the delimiter and ignore n?

I was looking at this. But unfortunately none of those solutions worked for me.

Any help would be greatly appreciated!

EDIT: For more clarification: I’m trying to do is split mysampletxt into two from the ; character.. And be able to insert the first part of the split text one place and the second into another.

Advertisement

Answer

echo -e "${mysampletxt%;*}nSome code goes herenMore coden${mysampletxt#*;}"

or

printf "%b" "${mysampletxt%;*}nSome code goes herenMore coden${mysampletxt#*;}"
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement