Computing.Net > Forums > Unix > To remove last occurance of character

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

To remove last occurance of character

Reply to Message Icon

Name: karunamoy kondru
Date: August 13, 2002 at 06:38:03 Pacific
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Frank
Date: August 13, 2002 at 06:43:48 Pacific
Reply:

Hi,

use:
sed -e "s/,$//" filename
This will remove the comma at the end of each line.

No RISK no fun
Frank


0

Response Number 2
Name: Jerry Lemieux
Date: August 13, 2002 at 17:24:28 Pacific
Reply:

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.file

while read -u3 Name
do
print "${Name%,*}"
done

Using 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



0

Response Number 3
Name: Frank
Date: August 13, 2002 at 22:33:42 Pacific
Reply:

looks god but had the disadvantage that it removes the last string not only the comma.

No RISK no fun
Frank


0

Response Number 4
Name: Jerry Lemieux
Date: August 14, 2002 at 06:33:33 Pacific
Reply:

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.


0

Response Number 5
Name: Frank
Date: August 14, 2002 at 07:06:54 Pacific
Reply:

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,klaus

Where 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


0

Related Posts

See More



Response Number 6
Name: Jerry Lemieux
Date: August 14, 2002 at 08:03:19 Pacific
Reply:

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.


0

Response Number 7
Name: Jerry Lemieux
Date: August 14, 2002 at 09:23:01 Pacific
Reply:

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
do

case $Name in
*, ) print "${Name%,*}"
;;
* ) print $Name
;;
esac

done

}

The output looks like this:

art,bill,jerry,tom
art,bill,jerry,tom
art,bill,jerry,tom

I 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



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: To remove last occurance of character

Append to last line of file www.computing.net/answers/unix/append-to-last-line-of-file/5212.html

count the occurence of a char in a string www.computing.net/answers/unix/count-the-occurence-of-a-char-in-a-string/8516.html

mt, or how to find last record www.computing.net/answers/unix/mt-or-how-to-find-last-record/4714.html