You can do it in a clean shell ...
$ foo="My Number is = 1234; #This is a random number" $ echo -n "${foo%%#*}#"; echo "${foo#*#}" | tr ' ' '+' My Number is = 1234;
Capturing this data in variables for later use is left as an exercise for the reader. :-)
Note that this also holds a few # characters per line:
$ foo="My Number is = 1234; #This is a # random number" $ echo -n "${foo%%#*}#"; echo "${foo#*#}" | tr ' ' '+' My Number is = 1234;
Or, if you prefer to create a variable over tr
:
$ echo -n "${foo%%#*}#"; bar="${foo#*#}"; echo "${bar// /+}" My Number is = 1234;
And finally, if you don't mind subshells with pipes, you can do this:
$ bar=$(echo -n "$foo" | tr '#' '\n' | sed -ne '2,$s/ /+/g;p' | tr '\n' '#') $ echo "$bar" My Number is = 1234;
And for fun, here's a short awk
solution:
$ echo $foo | awk -vRS=
Pay attention to the final ORS. I do not know if the final record separator can be avoided. I suppose you could get rid of this by plotting the line above through head -1
, assuming that you are dealing with only one line of input.
source share