To remove the second last character from a string

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I've a string something like this: abdh0chjkj0g.
I want to delete the second last character only if it is a 0.
I know sed and awk would do the work..but i'm unable to get the exact command for this.



Any help?







share|improve this question






















  • Should all zeros be removed from rcaoe0aoea0o and all B from aoBoaeBo? or is only the second to last character or zeros in general?
    – Kusalananda
    Apr 2 at 11:48











  • Hi, Only the 'zero' present in the second last position needs to be removed. Thanks
    – User123
    Apr 2 at 11:49














up vote
0
down vote

favorite












I've a string something like this: abdh0chjkj0g.
I want to delete the second last character only if it is a 0.
I know sed and awk would do the work..but i'm unable to get the exact command for this.



Any help?







share|improve this question






















  • Should all zeros be removed from rcaoe0aoea0o and all B from aoBoaeBo? or is only the second to last character or zeros in general?
    – Kusalananda
    Apr 2 at 11:48











  • Hi, Only the 'zero' present in the second last position needs to be removed. Thanks
    – User123
    Apr 2 at 11:49












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've a string something like this: abdh0chjkj0g.
I want to delete the second last character only if it is a 0.
I know sed and awk would do the work..but i'm unable to get the exact command for this.



Any help?







share|improve this question














I've a string something like this: abdh0chjkj0g.
I want to delete the second last character only if it is a 0.
I know sed and awk would do the work..but i'm unable to get the exact command for this.



Any help?









share|improve this question













share|improve this question




share|improve this question








edited Apr 2 at 11:53









cunninghamp3

473215




473215










asked Apr 2 at 11:40









User123

577




577











  • Should all zeros be removed from rcaoe0aoea0o and all B from aoBoaeBo? or is only the second to last character or zeros in general?
    – Kusalananda
    Apr 2 at 11:48











  • Hi, Only the 'zero' present in the second last position needs to be removed. Thanks
    – User123
    Apr 2 at 11:49
















  • Should all zeros be removed from rcaoe0aoea0o and all B from aoBoaeBo? or is only the second to last character or zeros in general?
    – Kusalananda
    Apr 2 at 11:48











  • Hi, Only the 'zero' present in the second last position needs to be removed. Thanks
    – User123
    Apr 2 at 11:49















Should all zeros be removed from rcaoe0aoea0o and all B from aoBoaeBo? or is only the second to last character or zeros in general?
– Kusalananda
Apr 2 at 11:48





Should all zeros be removed from rcaoe0aoea0o and all B from aoBoaeBo? or is only the second to last character or zeros in general?
– Kusalananda
Apr 2 at 11:48













Hi, Only the 'zero' present in the second last position needs to be removed. Thanks
– User123
Apr 2 at 11:49




Hi, Only the 'zero' present in the second last position needs to be removed. Thanks
– User123
Apr 2 at 11:49










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Using sed :



$ echo 'abdhchjkj0g' | sed 's/0(.)$/1/'


Using perl :



$ echo 'abdhchjkj0g' | perl -pe 's/0(?=.$)//'


Using gnuawk :



$ echo 'abdhchjkj0g' | awk 'print gensub(/0(.)$/, "\1", "1")'







abdhchjkjg





share|improve this answer






















  • I don't think this quite does what is asked for yet (I think because the example doesn't match the question) - you'll need to pull all 0s to match the question. For example, I think, dsgf0dsfads0d0d would need to become dsgfdsfadsdd
    – cunninghamp3
    Apr 2 at 11:46










  • OP says : the second last character
    – Gilles Quenot
    Apr 2 at 11:48











  • Then continues to say "from the string everytime it appears"
    – cunninghamp3
    Apr 2 at 11:50










  • And, as far as I can interpret his comment, only if it is a zero.
    – Kusalananda
    Apr 2 at 11:51






  • 1




    @Giles: Yeah :-) now all fine.Thanks
    – User123
    Apr 2 at 12:08










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f435031%2fto-remove-the-second-last-character-from-a-string%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Using sed :



$ echo 'abdhchjkj0g' | sed 's/0(.)$/1/'


Using perl :



$ echo 'abdhchjkj0g' | perl -pe 's/0(?=.$)//'


Using gnuawk :



$ echo 'abdhchjkj0g' | awk 'print gensub(/0(.)$/, "\1", "1")'







abdhchjkjg





share|improve this answer






















  • I don't think this quite does what is asked for yet (I think because the example doesn't match the question) - you'll need to pull all 0s to match the question. For example, I think, dsgf0dsfads0d0d would need to become dsgfdsfadsdd
    – cunninghamp3
    Apr 2 at 11:46










  • OP says : the second last character
    – Gilles Quenot
    Apr 2 at 11:48











  • Then continues to say "from the string everytime it appears"
    – cunninghamp3
    Apr 2 at 11:50










  • And, as far as I can interpret his comment, only if it is a zero.
    – Kusalananda
    Apr 2 at 11:51






  • 1




    @Giles: Yeah :-) now all fine.Thanks
    – User123
    Apr 2 at 12:08














up vote
2
down vote



accepted










Using sed :



$ echo 'abdhchjkj0g' | sed 's/0(.)$/1/'


Using perl :



$ echo 'abdhchjkj0g' | perl -pe 's/0(?=.$)//'


Using gnuawk :



$ echo 'abdhchjkj0g' | awk 'print gensub(/0(.)$/, "\1", "1")'







abdhchjkjg





share|improve this answer






















  • I don't think this quite does what is asked for yet (I think because the example doesn't match the question) - you'll need to pull all 0s to match the question. For example, I think, dsgf0dsfads0d0d would need to become dsgfdsfadsdd
    – cunninghamp3
    Apr 2 at 11:46










  • OP says : the second last character
    – Gilles Quenot
    Apr 2 at 11:48











  • Then continues to say "from the string everytime it appears"
    – cunninghamp3
    Apr 2 at 11:50










  • And, as far as I can interpret his comment, only if it is a zero.
    – Kusalananda
    Apr 2 at 11:51






  • 1




    @Giles: Yeah :-) now all fine.Thanks
    – User123
    Apr 2 at 12:08












up vote
2
down vote



accepted







up vote
2
down vote



accepted






Using sed :



$ echo 'abdhchjkj0g' | sed 's/0(.)$/1/'


Using perl :



$ echo 'abdhchjkj0g' | perl -pe 's/0(?=.$)//'


Using gnuawk :



$ echo 'abdhchjkj0g' | awk 'print gensub(/0(.)$/, "\1", "1")'







abdhchjkjg





share|improve this answer














Using sed :



$ echo 'abdhchjkj0g' | sed 's/0(.)$/1/'


Using perl :



$ echo 'abdhchjkj0g' | perl -pe 's/0(?=.$)//'


Using gnuawk :



$ echo 'abdhchjkj0g' | awk 'print gensub(/0(.)$/, "\1", "1")'







abdhchjkjg






share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 2 at 12:09

























answered Apr 2 at 11:43









Gilles Quenot

15.3k13448




15.3k13448











  • I don't think this quite does what is asked for yet (I think because the example doesn't match the question) - you'll need to pull all 0s to match the question. For example, I think, dsgf0dsfads0d0d would need to become dsgfdsfadsdd
    – cunninghamp3
    Apr 2 at 11:46










  • OP says : the second last character
    – Gilles Quenot
    Apr 2 at 11:48











  • Then continues to say "from the string everytime it appears"
    – cunninghamp3
    Apr 2 at 11:50










  • And, as far as I can interpret his comment, only if it is a zero.
    – Kusalananda
    Apr 2 at 11:51






  • 1




    @Giles: Yeah :-) now all fine.Thanks
    – User123
    Apr 2 at 12:08
















  • I don't think this quite does what is asked for yet (I think because the example doesn't match the question) - you'll need to pull all 0s to match the question. For example, I think, dsgf0dsfads0d0d would need to become dsgfdsfadsdd
    – cunninghamp3
    Apr 2 at 11:46










  • OP says : the second last character
    – Gilles Quenot
    Apr 2 at 11:48











  • Then continues to say "from the string everytime it appears"
    – cunninghamp3
    Apr 2 at 11:50










  • And, as far as I can interpret his comment, only if it is a zero.
    – Kusalananda
    Apr 2 at 11:51






  • 1




    @Giles: Yeah :-) now all fine.Thanks
    – User123
    Apr 2 at 12:08















I don't think this quite does what is asked for yet (I think because the example doesn't match the question) - you'll need to pull all 0s to match the question. For example, I think, dsgf0dsfads0d0d would need to become dsgfdsfadsdd
– cunninghamp3
Apr 2 at 11:46




I don't think this quite does what is asked for yet (I think because the example doesn't match the question) - you'll need to pull all 0s to match the question. For example, I think, dsgf0dsfads0d0d would need to become dsgfdsfadsdd
– cunninghamp3
Apr 2 at 11:46












OP says : the second last character
– Gilles Quenot
Apr 2 at 11:48





OP says : the second last character
– Gilles Quenot
Apr 2 at 11:48













Then continues to say "from the string everytime it appears"
– cunninghamp3
Apr 2 at 11:50




Then continues to say "from the string everytime it appears"
– cunninghamp3
Apr 2 at 11:50












And, as far as I can interpret his comment, only if it is a zero.
– Kusalananda
Apr 2 at 11:51




And, as far as I can interpret his comment, only if it is a zero.
– Kusalananda
Apr 2 at 11:51




1




1




@Giles: Yeah :-) now all fine.Thanks
– User123
Apr 2 at 12:08




@Giles: Yeah :-) now all fine.Thanks
– User123
Apr 2 at 12:08












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f435031%2fto-remove-the-second-last-character-from-a-string%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay