Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9135557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:50:25+00:00 2026-06-17T08:50:25+00:00

How can I remove the last n characters from a particular string using shell

  • 0

How can I remove the last n characters from a particular string using shell script?

This is my input:

ssl01:49188,,,
ssl01:49188,
ssl01:49188,,,,,
ssl01:49188,ssl999999:49188,,,,,
ssl01:49188,abcf999:49188,,,,,

The output should be in the following format:

ssl01:49188
ssl01:49188
ssl01:49188
ssl01:49188,ssl999999:49188
ssl01:49188,abcf999:49188
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:50:26+00:00Added an answer on June 17, 2026 at 8:50 am

    To answer the title of you question with specifies cutting last n character in a string, you can use the substring extraction feature in Bash.

    me@home$ A="123456"
    me@home$ echo ${A:0:-2}  # remove last 2 chars
    1234
    

    However, based on your examples you appear to want to remove all trailing commas, in which case you could use sed 's/,*$//'.

    me@home$ echo "ssl01:49188,ssl999999:49188,,,,," | sed 's/,*$//'
    ssl01:49188,ssl999999:49188
    

    or, for a purely Bash solution, you could use substring removal:

    me@home$ X="ssl01:49188,ssl999999:49188,,,,,"
    me@home$ shopt -s extglob
    me@home$ echo ${X%%+(,)}
    ssl01:49188,ssl999999:49188
    

    I would use the sed approach if the transformation needs to be applied to a whole file, and the bash substring removal approach if the target string is already in a bash variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to remove the last 3 characters from a string in Python, I
How can I remove last character from a C++ string? I tried st =
How can I remove the last word in the string using JavaScript? For example,
How can I remove a file from my last commit, but keep everything else?
I have a DIV with some characters. How can I remove the last character
I want to remove all strange characters from a string to make it url
I'm using this function to remove http://, https:// and www. from my URLs. function
I know I can remove the last element from a set: s.Remove(s.MaximumElement) But if
I can add and remove the last line in my dynamic form and calculate
we can remove formula from one cell by cell.setCellFormula(null) . but if i want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.