Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
My file has one record with data separated by commas. I want to remove the last comma. How do I do it in a script?
Thanks in advance
Karuna

Hi,
use:
sed -e "s/,$//" filename
This will remove the comma at the end of each line.No RISK no fun
Frank

Given a file name /tmp/data.file with the following entries:
art,bill,jerry,tom,
You can do it in the shell like this:
#!/bin/ksh
exec 3< /tmp/data.filewhile read -u3 Name
do
print "${Name%,*}"
doneUsing sed is nice, however, if you are writing a script and can do what you want in the shell, you eliminate an external call to a utility.
Jerry

looks god but had the disadvantage that it removes the last string not only the comma.
No RISK no fun
Frank

On my Solaris 8 system, using the Korn shell and given a file with this entry:
art,bill,jerry,tom,
I got the following output:
art,bill,jerry,tom
Exactly what got removed that the poster wasn't trying to get rid of? Appeared to me that the poster wanted to get rid of the trailing comma, which is what this does. Syntax will delete the shortest suffix portion of the matched pattern. In this case, the ",*" will only match the last comma in the record. sed is a powerful utility, but it is overkill for such a simple task.
An extract from the man page, for your information:
${parameter%word}
Remove Smallest Suffix Pattern. The word will be expanded to produce a pattern. The parameter expansion then will result in parameter, with the smallest portion of the suffix matched by the pattern deleted.

Hi Jerry,
I fully agree on the usage of sed
Anyway be aware of the following:
your file containes two lines e.g.:david, jerry
hans,klaus,
The out put of your script would be:david
hans,klausWhere is jerry ?
Your suggested solution would remove more than only the last comma.
To prevent this and not use the sed would be
(based on your script)
print "${Name%,}"
instead of print "${Name%,*}"No RISK no fun
Frank

The posting said:
"My file has one record with data separated by commas"
That is the reason I posted my solution.
If he had stated multiple records were in the file and that some had commas at the end and some didn't, I would have proposed a difference solution or not commented at all. Your sed solution would have been the most approprite in that situation. sed is my preference in every case over awk where there is an option to use either.

Two ways to accomplish this where there are different records, some ending in a comma and some not.
data.file
art,bill,jerry,tom,
art,bill,jerry,tom
art,bill,jerry,tom,Using an if statement in the Korn shell:
#!/bin/ksh
exec 3< /tmp/data.file
{
while read -u3 Name
do
if [[ $Name = *, ]]
then
print "${Name%,*}"
else
print $Name
fi
done
}Using a case statement in the Korn shell:
#!/bin/ksh
exec 3< /tmp/data.file
{
while read -u3 Name
docase $Name in
*, ) print "${Name%,*}"
;;
* ) print $Name
;;
esacdone
}
The output looks like this:
art,bill,jerry,tom
art,bill,jerry,tom
art,bill,jerry,tomI could probably come up with some other solutions that do not require calling a utility program to perform the task. These solutions keep all the work within the shell.
Jerry

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |