Add Space Before Last Character in Variable
up vote
1
down vote
favorite
I have a variable that contains some numbers then a letter
example: 242M
I need to add a space right before the last character in that variable so that when echo'd it would look like 242 M
I've tried sed but it looks like from what I can find that only works with reading from a file and I haven't been able to get this to work modifying a variable.
Thanks!
bash shell-script scripting variable
New contributor
add a comment |
up vote
1
down vote
favorite
I have a variable that contains some numbers then a letter
example: 242M
I need to add a space right before the last character in that variable so that when echo'd it would look like 242 M
I've tried sed but it looks like from what I can find that only works with reading from a file and I haven't been able to get this to work modifying a variable.
Thanks!
bash shell-script scripting variable
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a variable that contains some numbers then a letter
example: 242M
I need to add a space right before the last character in that variable so that when echo'd it would look like 242 M
I've tried sed but it looks like from what I can find that only works with reading from a file and I haven't been able to get this to work modifying a variable.
Thanks!
bash shell-script scripting variable
New contributor
I have a variable that contains some numbers then a letter
example: 242M
I need to add a space right before the last character in that variable so that when echo'd it would look like 242 M
I've tried sed but it looks like from what I can find that only works with reading from a file and I haven't been able to get this to work modifying a variable.
Thanks!
bash shell-script scripting variable
bash shell-script scripting variable
New contributor
New contributor
New contributor
asked Dec 5 at 19:19
RyanL
82
82
New contributor
New contributor
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
2
down vote
accepted
A version that should work on any POSIX shell, not just bash
:
printf '%sn' "${a%?} ${a#"${a%?}"}"
The extra quotes inside ${a#...}
are to protect against special characters from the variable:
$ a='10*M'; printf '%sn' "${a%?} ${a#"${a%?}"}"
10* M
Without them, the second variable expansion would've turned into ${a#10*}
, and 10* *M
would've been echoed instead.
add a comment |
up vote
1
down vote
If the other answer doesn't work (e.g. on macOS), you can also try
a="${a%?} ${a: -1}"
which puts together a without its last character, a space, and the last character of a.
New contributor
add a comment |
up vote
0
down vote
Purely within the shell:
var=${var:0:-1}' '${var: -1:1}
For example:
$ var=jeff
$ var=${var:0:-1}' '${var: -1:1}
$ printf '%sn' "$var"
jef f
add a comment |
up vote
0
down vote
Here is a Sed example that works with a variable:
q=242M
sed 's/.$/ &/' <<eof
$q
eof
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
A version that should work on any POSIX shell, not just bash
:
printf '%sn' "${a%?} ${a#"${a%?}"}"
The extra quotes inside ${a#...}
are to protect against special characters from the variable:
$ a='10*M'; printf '%sn' "${a%?} ${a#"${a%?}"}"
10* M
Without them, the second variable expansion would've turned into ${a#10*}
, and 10* *M
would've been echoed instead.
add a comment |
up vote
2
down vote
accepted
A version that should work on any POSIX shell, not just bash
:
printf '%sn' "${a%?} ${a#"${a%?}"}"
The extra quotes inside ${a#...}
are to protect against special characters from the variable:
$ a='10*M'; printf '%sn' "${a%?} ${a#"${a%?}"}"
10* M
Without them, the second variable expansion would've turned into ${a#10*}
, and 10* *M
would've been echoed instead.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
A version that should work on any POSIX shell, not just bash
:
printf '%sn' "${a%?} ${a#"${a%?}"}"
The extra quotes inside ${a#...}
are to protect against special characters from the variable:
$ a='10*M'; printf '%sn' "${a%?} ${a#"${a%?}"}"
10* M
Without them, the second variable expansion would've turned into ${a#10*}
, and 10* *M
would've been echoed instead.
A version that should work on any POSIX shell, not just bash
:
printf '%sn' "${a%?} ${a#"${a%?}"}"
The extra quotes inside ${a#...}
are to protect against special characters from the variable:
$ a='10*M'; printf '%sn' "${a%?} ${a#"${a%?}"}"
10* M
Without them, the second variable expansion would've turned into ${a#10*}
, and 10* *M
would've been echoed instead.
edited Dec 5 at 22:50
Stéphane Chazelas
296k54559904
296k54559904
answered Dec 5 at 21:58
mosvy
5,1791323
5,1791323
add a comment |
add a comment |
up vote
1
down vote
If the other answer doesn't work (e.g. on macOS), you can also try
a="${a%?} ${a: -1}"
which puts together a without its last character, a space, and the last character of a.
New contributor
add a comment |
up vote
1
down vote
If the other answer doesn't work (e.g. on macOS), you can also try
a="${a%?} ${a: -1}"
which puts together a without its last character, a space, and the last character of a.
New contributor
add a comment |
up vote
1
down vote
up vote
1
down vote
If the other answer doesn't work (e.g. on macOS), you can also try
a="${a%?} ${a: -1}"
which puts together a without its last character, a space, and the last character of a.
New contributor
If the other answer doesn't work (e.g. on macOS), you can also try
a="${a%?} ${a: -1}"
which puts together a without its last character, a space, and the last character of a.
New contributor
New contributor
answered Dec 5 at 19:32
Jim Danner
112
112
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
Purely within the shell:
var=${var:0:-1}' '${var: -1:1}
For example:
$ var=jeff
$ var=${var:0:-1}' '${var: -1:1}
$ printf '%sn' "$var"
jef f
add a comment |
up vote
0
down vote
Purely within the shell:
var=${var:0:-1}' '${var: -1:1}
For example:
$ var=jeff
$ var=${var:0:-1}' '${var: -1:1}
$ printf '%sn' "$var"
jef f
add a comment |
up vote
0
down vote
up vote
0
down vote
Purely within the shell:
var=${var:0:-1}' '${var: -1:1}
For example:
$ var=jeff
$ var=${var:0:-1}' '${var: -1:1}
$ printf '%sn' "$var"
jef f
Purely within the shell:
var=${var:0:-1}' '${var: -1:1}
For example:
$ var=jeff
$ var=${var:0:-1}' '${var: -1:1}
$ printf '%sn' "$var"
jef f
answered Dec 5 at 19:23
Jeff Schaller
37.4k1052121
37.4k1052121
add a comment |
add a comment |
up vote
0
down vote
Here is a Sed example that works with a variable:
q=242M
sed 's/.$/ &/' <<eof
$q
eof
add a comment |
up vote
0
down vote
Here is a Sed example that works with a variable:
q=242M
sed 's/.$/ &/' <<eof
$q
eof
add a comment |
up vote
0
down vote
up vote
0
down vote
Here is a Sed example that works with a variable:
q=242M
sed 's/.$/ &/' <<eof
$q
eof
Here is a Sed example that works with a variable:
q=242M
sed 's/.$/ &/' <<eof
$q
eof
answered Dec 6 at 2:45
Steven Penny
2,48221737
2,48221737
add a comment |
add a comment |
RyanL is a new contributor. Be nice, and check out our Code of Conduct.
RyanL is a new contributor. Be nice, and check out our Code of Conduct.
RyanL is a new contributor. Be nice, and check out our Code of Conduct.
RyanL is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f486218%2fadd-space-before-last-character-in-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown