Operating System - HP-UX
1827741 Members
3190 Online
109969 Solutions
New Discussion

Re: remove comma (,) from end of the line ?

 
SOLVED
Go to solution
Sammy_2
Super Advisor

remove comma (,) from end of the line ?

#cat data.txt
123,456,2323,23232,


1) Using either shell or perl command,How do I remove the last comma after 23232 ?


2)When I try to view it (vi data.txt), I get
"data.txt" Line too long
message. How do I edit the file ?
The file actually has about 1200 numbers separated with comma.
good judgement comes from experience and experience comes from bad judgement.
8 REPLIES 8
Peter Godron
Honored Contributor
Solution

Re: remove comma (,) from end of the line ?

Hi,
1.
sed "1,$ s/,$//" file > file2
Peter Godron
Honored Contributor

Re: remove comma (,) from end of the line ?

Hi again,
please no points for this second part.
vi has a limit of 2048 (or now 4096) characters on one line. Solutions seems to be to either split the file or use alternate editor.
See
http://forums1.itrc.hp.com/service/forums/bizsupport/questionanswer.do?threadId=33157
Sammy_2
Super Advisor

Re: remove comma (,) from end of the line ?

Peter,
Thanks.

Your command works beautifully on a small file.
# sed "1,$ s/,$//" try
123,456,2323,23232


But this file, which has one line of 9490 characters yields nothing.
# wc -m data.txt
9490 data.txt

# sed "1,$ s/,$//" data.txt
This yields nothing.
I have to run more command to look at the file. Could be file length problem again (I guess).

Attaching data.txt file as well.
good judgement comes from experience and experience comes from bad judgement.
Peter Nikitka
Honored Contributor

Re: remove comma (,) from end of the line ?

Hi,

what about
perl -pne 's/,$//;' data.txt

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: remove comma (,) from end of the line ?

Hi Sammy:

Use Perl;

# perl -pe 's/,$//' file

...or to update "inplace":

# perl -pi.old -e 's/,$//' file

That last variation creates a backup of "file" as "file.old" and recreates "file" to your specification.

Regards!

...JRF...
Sammy_2
Super Advisor

Re: remove comma (,) from end of the line ?

Must be a Peter Day for me today.
Peter G,
Thanks for the link as i am going to split the file and then sed will probably work.

Peter N,
You are right on because the Pete G. sed command would not parse this (9490 characters) long file but your perl command did as the data1.txt file
does not have comma. I am viewing this file by doing more and not vi.

perl -pne 's/,$//;' data.txt > data1.txt
good judgement comes from experience and experience comes from bad judgement.
Sammy_2
Super Advisor

Re: remove comma (,) from end of the line ?

Thanks JRF as well as your perl commands always add a interesting twist. Peter N already had something similiar to you have but I like the variations.
good judgement comes from experience and experience comes from bad judgement.
H.Merijn Brand (procura
Honored Contributor

Re: remove comma (,) from end of the line ?

I know it's closed, but to warn off with Peter's answer

# perl -pne 's/,$//;' data.txt

is wrong, as the options -n and -p are contradicting

# perl -pe's/,$//' data.txt

is the correct syntax, and indeed for the in-file change

# perl -pi -e's/,$//' data.txt

making it even more defensive:

# perl -pi -e's/,\s*$//' data.txt

allows optional trailing whitespace (which will also be removed)

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn