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
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.
However, based on your examples you appear to want to remove all trailing commas, in which case you could use
sed 's/,*$//'.or, for a purely Bash solution, you could use substring removal:
I would use the
sedapproach 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.