How do I remove the last characters from a string?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I have a variable set with var='type_cardio_10-11-2017'
. I need to remove the last 10 letters from the variable, and append the remaining value to var2
.
I tried with the following script, but it doesn't work as expected.
var=type_cardio_10-11-2017
var2=$var | cut -f1 -d "_"
echo $var2
The output I want is type_cardio.
shell text-processing variable cut
add a comment |Â
up vote
6
down vote
favorite
I have a variable set with var='type_cardio_10-11-2017'
. I need to remove the last 10 letters from the variable, and append the remaining value to var2
.
I tried with the following script, but it doesn't work as expected.
var=type_cardio_10-11-2017
var2=$var | cut -f1 -d "_"
echo $var2
The output I want is type_cardio.
shell text-processing variable cut
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I have a variable set with var='type_cardio_10-11-2017'
. I need to remove the last 10 letters from the variable, and append the remaining value to var2
.
I tried with the following script, but it doesn't work as expected.
var=type_cardio_10-11-2017
var2=$var | cut -f1 -d "_"
echo $var2
The output I want is type_cardio.
shell text-processing variable cut
I have a variable set with var='type_cardio_10-11-2017'
. I need to remove the last 10 letters from the variable, and append the remaining value to var2
.
I tried with the following script, but it doesn't work as expected.
var=type_cardio_10-11-2017
var2=$var | cut -f1 -d "_"
echo $var2
The output I want is type_cardio.
shell text-processing variable cut
edited Oct 20 '17 at 17:57
Jeff Schaller
32.1k849109
32.1k849109
asked Oct 20 '17 at 17:38
Rak kundra
141110
141110
add a comment |Â
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
14
down vote
accepted
To remove everything from after the last _
in var
and assign the result to var2
:
var2=$var%_*
The parameter expansion $parameter%word
removes the pattern word
(_*
in this case) from the end of the value of the given variable.
The POSIX standard calls this a "Remove Smallest Suffix Pattern" parameter expansion.
add a comment |Â
up vote
9
down vote
You actually want to remove the trailing 11 characters from the string; here's another way to do it:
$ var=type_cardio_10-11-2017
$ var2=$var%???????????
$ echo "$var2"
type_cardio
add a comment |Â
up vote
5
down vote
Another approach in bash
:
echo "$var::-10"
Or in older versions:
echo "$var::$#var-10" #or
echo "$var: : -10"
add a comment |Â
up vote
4
down vote
1) bash solution:
var1="type_cardio_10-11-2017"
var2=$var1%_*
2) cut solution:
var1="type_cardio_10-11-2017"
var2=$(cut -d'_' -f1,2 <<<"$var1")
echo "$var2"
The output:
type_cardio
Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
â Rak kundra
Oct 20 '17 at 17:44
1
@SanthoshPogaku, that was not clear from your initial question, you have my update
â RomanPerekhrest
Oct 20 '17 at 17:52
Yeah sir, I got what am i looking for . Thank's for your valuable time.
â Rak kundra
Oct 20 '17 at 17:53
add a comment |Â
up vote
1
down vote
Appending var
and var2
is the easy part - you can just join them like new_var="$var$var2"
. If what you really meant to say is you want to store cropped var
into var2
,then it's just var2=$(...)
and you can put the commands that other users and my answer presented here inside the $()
portion.
The main part is removing those 10 characters. There's number of ways to extract the part you want from var
:
printf
(pretty portable, doesn't rely on specific shell)$ printf "%.11sn" "$var"
type_cardioawk
$ var=type_cardio_10-11-2017
$ awk -v awk_var="$var" 'BEGINprint substr(awk_var,0,length(awk_var)-11)'
type_cardioor you can use
printf
trick here as well:$ echo "$var" | awk 'printf "%.11sn",$0'
type_cardioegrep
$ echo "$var" | egrep -o '^.0,11'
type_cardioperl
:$ echo "$var" | perl -lne 'print substr($_,0,11)'
type_cardiopython
:$ python -c 'import sys;print sys.argv[1][0:11] ' "$var"
type_cardio
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
14
down vote
accepted
To remove everything from after the last _
in var
and assign the result to var2
:
var2=$var%_*
The parameter expansion $parameter%word
removes the pattern word
(_*
in this case) from the end of the value of the given variable.
The POSIX standard calls this a "Remove Smallest Suffix Pattern" parameter expansion.
add a comment |Â
up vote
14
down vote
accepted
To remove everything from after the last _
in var
and assign the result to var2
:
var2=$var%_*
The parameter expansion $parameter%word
removes the pattern word
(_*
in this case) from the end of the value of the given variable.
The POSIX standard calls this a "Remove Smallest Suffix Pattern" parameter expansion.
add a comment |Â
up vote
14
down vote
accepted
up vote
14
down vote
accepted
To remove everything from after the last _
in var
and assign the result to var2
:
var2=$var%_*
The parameter expansion $parameter%word
removes the pattern word
(_*
in this case) from the end of the value of the given variable.
The POSIX standard calls this a "Remove Smallest Suffix Pattern" parameter expansion.
To remove everything from after the last _
in var
and assign the result to var2
:
var2=$var%_*
The parameter expansion $parameter%word
removes the pattern word
(_*
in this case) from the end of the value of the given variable.
The POSIX standard calls this a "Remove Smallest Suffix Pattern" parameter expansion.
edited Oct 20 '17 at 17:55
answered Oct 20 '17 at 17:49
Kusalananda
105k14209326
105k14209326
add a comment |Â
add a comment |Â
up vote
9
down vote
You actually want to remove the trailing 11 characters from the string; here's another way to do it:
$ var=type_cardio_10-11-2017
$ var2=$var%???????????
$ echo "$var2"
type_cardio
add a comment |Â
up vote
9
down vote
You actually want to remove the trailing 11 characters from the string; here's another way to do it:
$ var=type_cardio_10-11-2017
$ var2=$var%???????????
$ echo "$var2"
type_cardio
add a comment |Â
up vote
9
down vote
up vote
9
down vote
You actually want to remove the trailing 11 characters from the string; here's another way to do it:
$ var=type_cardio_10-11-2017
$ var2=$var%???????????
$ echo "$var2"
type_cardio
You actually want to remove the trailing 11 characters from the string; here's another way to do it:
$ var=type_cardio_10-11-2017
$ var2=$var%???????????
$ echo "$var2"
type_cardio
answered Oct 20 '17 at 17:56
Jeff Schaller
32.1k849109
32.1k849109
add a comment |Â
add a comment |Â
up vote
5
down vote
Another approach in bash
:
echo "$var::-10"
Or in older versions:
echo "$var::$#var-10" #or
echo "$var: : -10"
add a comment |Â
up vote
5
down vote
Another approach in bash
:
echo "$var::-10"
Or in older versions:
echo "$var::$#var-10" #or
echo "$var: : -10"
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Another approach in bash
:
echo "$var::-10"
Or in older versions:
echo "$var::$#var-10" #or
echo "$var: : -10"
Another approach in bash
:
echo "$var::-10"
Or in older versions:
echo "$var::$#var-10" #or
echo "$var: : -10"
edited Oct 20 '17 at 18:07
answered Oct 20 '17 at 18:00
ñÃÂsýù÷
15.6k92563
15.6k92563
add a comment |Â
add a comment |Â
up vote
4
down vote
1) bash solution:
var1="type_cardio_10-11-2017"
var2=$var1%_*
2) cut solution:
var1="type_cardio_10-11-2017"
var2=$(cut -d'_' -f1,2 <<<"$var1")
echo "$var2"
The output:
type_cardio
Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
â Rak kundra
Oct 20 '17 at 17:44
1
@SanthoshPogaku, that was not clear from your initial question, you have my update
â RomanPerekhrest
Oct 20 '17 at 17:52
Yeah sir, I got what am i looking for . Thank's for your valuable time.
â Rak kundra
Oct 20 '17 at 17:53
add a comment |Â
up vote
4
down vote
1) bash solution:
var1="type_cardio_10-11-2017"
var2=$var1%_*
2) cut solution:
var1="type_cardio_10-11-2017"
var2=$(cut -d'_' -f1,2 <<<"$var1")
echo "$var2"
The output:
type_cardio
Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
â Rak kundra
Oct 20 '17 at 17:44
1
@SanthoshPogaku, that was not clear from your initial question, you have my update
â RomanPerekhrest
Oct 20 '17 at 17:52
Yeah sir, I got what am i looking for . Thank's for your valuable time.
â Rak kundra
Oct 20 '17 at 17:53
add a comment |Â
up vote
4
down vote
up vote
4
down vote
1) bash solution:
var1="type_cardio_10-11-2017"
var2=$var1%_*
2) cut solution:
var1="type_cardio_10-11-2017"
var2=$(cut -d'_' -f1,2 <<<"$var1")
echo "$var2"
The output:
type_cardio
1) bash solution:
var1="type_cardio_10-11-2017"
var2=$var1%_*
2) cut solution:
var1="type_cardio_10-11-2017"
var2=$(cut -d'_' -f1,2 <<<"$var1")
echo "$var2"
The output:
type_cardio
edited Oct 20 '17 at 17:54
answered Oct 20 '17 at 17:42
RomanPerekhrest
22.5k12145
22.5k12145
Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
â Rak kundra
Oct 20 '17 at 17:44
1
@SanthoshPogaku, that was not clear from your initial question, you have my update
â RomanPerekhrest
Oct 20 '17 at 17:52
Yeah sir, I got what am i looking for . Thank's for your valuable time.
â Rak kundra
Oct 20 '17 at 17:53
add a comment |Â
Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
â Rak kundra
Oct 20 '17 at 17:44
1
@SanthoshPogaku, that was not clear from your initial question, you have my update
â RomanPerekhrest
Oct 20 '17 at 17:52
Yeah sir, I got what am i looking for . Thank's for your valuable time.
â Rak kundra
Oct 20 '17 at 17:53
Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
â Rak kundra
Oct 20 '17 at 17:44
Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
â Rak kundra
Oct 20 '17 at 17:44
1
1
@SanthoshPogaku, that was not clear from your initial question, you have my update
â RomanPerekhrest
Oct 20 '17 at 17:52
@SanthoshPogaku, that was not clear from your initial question, you have my update
â RomanPerekhrest
Oct 20 '17 at 17:52
Yeah sir, I got what am i looking for . Thank's for your valuable time.
â Rak kundra
Oct 20 '17 at 17:53
Yeah sir, I got what am i looking for . Thank's for your valuable time.
â Rak kundra
Oct 20 '17 at 17:53
add a comment |Â
up vote
1
down vote
Appending var
and var2
is the easy part - you can just join them like new_var="$var$var2"
. If what you really meant to say is you want to store cropped var
into var2
,then it's just var2=$(...)
and you can put the commands that other users and my answer presented here inside the $()
portion.
The main part is removing those 10 characters. There's number of ways to extract the part you want from var
:
printf
(pretty portable, doesn't rely on specific shell)$ printf "%.11sn" "$var"
type_cardioawk
$ var=type_cardio_10-11-2017
$ awk -v awk_var="$var" 'BEGINprint substr(awk_var,0,length(awk_var)-11)'
type_cardioor you can use
printf
trick here as well:$ echo "$var" | awk 'printf "%.11sn",$0'
type_cardioegrep
$ echo "$var" | egrep -o '^.0,11'
type_cardioperl
:$ echo "$var" | perl -lne 'print substr($_,0,11)'
type_cardiopython
:$ python -c 'import sys;print sys.argv[1][0:11] ' "$var"
type_cardio
add a comment |Â
up vote
1
down vote
Appending var
and var2
is the easy part - you can just join them like new_var="$var$var2"
. If what you really meant to say is you want to store cropped var
into var2
,then it's just var2=$(...)
and you can put the commands that other users and my answer presented here inside the $()
portion.
The main part is removing those 10 characters. There's number of ways to extract the part you want from var
:
printf
(pretty portable, doesn't rely on specific shell)$ printf "%.11sn" "$var"
type_cardioawk
$ var=type_cardio_10-11-2017
$ awk -v awk_var="$var" 'BEGINprint substr(awk_var,0,length(awk_var)-11)'
type_cardioor you can use
printf
trick here as well:$ echo "$var" | awk 'printf "%.11sn",$0'
type_cardioegrep
$ echo "$var" | egrep -o '^.0,11'
type_cardioperl
:$ echo "$var" | perl -lne 'print substr($_,0,11)'
type_cardiopython
:$ python -c 'import sys;print sys.argv[1][0:11] ' "$var"
type_cardio
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Appending var
and var2
is the easy part - you can just join them like new_var="$var$var2"
. If what you really meant to say is you want to store cropped var
into var2
,then it's just var2=$(...)
and you can put the commands that other users and my answer presented here inside the $()
portion.
The main part is removing those 10 characters. There's number of ways to extract the part you want from var
:
printf
(pretty portable, doesn't rely on specific shell)$ printf "%.11sn" "$var"
type_cardioawk
$ var=type_cardio_10-11-2017
$ awk -v awk_var="$var" 'BEGINprint substr(awk_var,0,length(awk_var)-11)'
type_cardioor you can use
printf
trick here as well:$ echo "$var" | awk 'printf "%.11sn",$0'
type_cardioegrep
$ echo "$var" | egrep -o '^.0,11'
type_cardioperl
:$ echo "$var" | perl -lne 'print substr($_,0,11)'
type_cardiopython
:$ python -c 'import sys;print sys.argv[1][0:11] ' "$var"
type_cardio
Appending var
and var2
is the easy part - you can just join them like new_var="$var$var2"
. If what you really meant to say is you want to store cropped var
into var2
,then it's just var2=$(...)
and you can put the commands that other users and my answer presented here inside the $()
portion.
The main part is removing those 10 characters. There's number of ways to extract the part you want from var
:
printf
(pretty portable, doesn't rely on specific shell)$ printf "%.11sn" "$var"
type_cardioawk
$ var=type_cardio_10-11-2017
$ awk -v awk_var="$var" 'BEGINprint substr(awk_var,0,length(awk_var)-11)'
type_cardioor you can use
printf
trick here as well:$ echo "$var" | awk 'printf "%.11sn",$0'
type_cardioegrep
$ echo "$var" | egrep -o '^.0,11'
type_cardioperl
:$ echo "$var" | perl -lne 'print substr($_,0,11)'
type_cardiopython
:$ python -c 'import sys;print sys.argv[1][0:11] ' "$var"
type_cardio
edited Oct 21 '17 at 17:20
answered Oct 21 '17 at 17:15
Sergiy Kolodyazhnyy
7,91011548
7,91011548
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399392%2fhow-do-i-remove-the-last-characters-from-a-string%23new-answer', 'question_page');
);
Post as a guest
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
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
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