I've a string something like this: abdh0chjkj0g. I want to delete the second last character only if it is a 0.

  • 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 '18 at 11:48
  • Hi, Only the 'zero' present in the second last position needs to be removed. Thanks – User123 Apr 2 '18 at 11:49

Using :

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

Using :

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

Using :

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

  • 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 '18 at 11:46
  • OP says : the second last character – Gilles Quenot Apr 2 '18 at 11:48
  • Then continues to say "from the string everytime it appears" – cunninghamp3 Apr 2 '18 at 11:50
  • And, as far as I can interpret his comment, only if it is a zero. – Kusalananda Apr 2 '18 at 11:51
  • @cunninghamp3 Possibly with the qualifier "at that position". Note "everytime", not "everywhere". – Kusalananda Apr 2 '18 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.