LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-15-2016, 07:35 PM   #1
freeroute
LQ Newbie
 
Registered: Jul 2016
Posts: 28

Rep: Reputation: Disabled
How to remove commas at the end of rows


I would like to remove commas , at the end of each line in my file.
sed only remove the last comma character, but I would like to remove all the commas (and maybe all other character, like:: at the end of a lines.
My command: sed 's/,$//' file

for example my file contains:
Solaris,
Debian,,,,,
Kubuntu,,
Unix,,,,,,,,
Apache:::

Thanks in advance.

Last edited by freeroute; 10-15-2016 at 07:46 PM.
 
Old 10-15-2016, 08:06 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,116

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
Pretty straightforward.
Code:
sed 's/,*$//' file                 # remove all trailing commas
sed 's/[,:]*$//' file              # remove all trailing commas and colons
sed 's/[^[:alnum:]]*$//' file      # remove all trailing non-alphanumeric characters
 
2 members found this post helpful.
Old 10-15-2016, 08:15 PM   #3
freeroute
LQ Newbie
 
Registered: Jul 2016
Posts: 28

Original Poster
Rep: Reputation: Disabled
Thank you very much.
 
Old 10-15-2016, 08:31 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
sorry. mixed up the patterns.

Last edited by jpollard; 10-15-2016 at 08:34 PM.
 
Old 10-17-2016, 02:09 PM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,094

Rep: Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515
Old style sed regex using () instead of [], removing all trailing commas and colons:

Code:
sed 's/\(,\|:\)*$//' input
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to remove commas from user's full name in passwd yangou Programming 3 04-30-2014 07:30 AM
Vi/ViM - how to remove commas between quotes Micro420 Programming 6 09-25-2009 01:40 AM
remove no data rows from a text file lothario Linux - Newbie 1 08-30-2009 08:18 AM
Bash: How to remove rows in CSVs where Price column is less than 30 guest Programming 10 04-11-2009 07:44 PM
Compare two fields on consecutive rows and print the two rows aditi_borkar Linux - Newbie 3 04-09-2009 06:49 AM


All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration