LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-20-2012, 02:27 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
sed - eliminate punctuation at start and/or end of each line


Eliminating punctuation marks everywhere in a file is easy enough.
I want to eliminate punctuation marks only at the start (or end) (or start and end) of each line.
How is this done?

I've tried constructs such as
Code:
^[:punct:]
and
Code:
[:punct:]$
to no avail.

Daniel B. Martin
 
Old 01-20-2012, 02:58 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Note that character classes only work within a definition, i.e. use [[:punct:]] or [[:punct:][:space:][:cntrl:]] for example. The classes depend on locale ( LANG, and LC_ALL or LC_CTYPE environment variables). Which characters do you consider punctuation? Or is the locale definition okay by you?

For me,
Code:
sed -e 's|^[[:punct:]]*||; s|[[:punct:]]*$||;' -i file-to-edit
works fine using GNU sed 4.2.1.
 
1 members found this post helpful.
Old 01-20-2012, 03:04 PM   #3
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 211

Rep: Reputation: 67
Please post how you have been using those constructs ... oh. a-z0-9 in [a-z0-9] names two ranges of code points. [:punct:] in [[:punct:]] names a set of code points. Your [:punct:] names the char class consisting of :, p, u, n, c, and t. You want the doubled brackets.

(edit: yup, n.a. said it.)

Last edited by jthill; 01-20-2012 at 03:04 PM. Reason: cwcid
 
1 members found this post helpful.
Old 01-20-2012, 03:15 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
[QUOTE=Nominal Animal;4580122]
Code:
sed -e 's|^[[:punct:]]*||; s|[[:punct:]]*$||;' -i file-to-edit
Perfect!


Quote:
Originally Posted by Nominal Animal View Post
Which characters do you consider punctuation? Or is the locale definition okay by you?
Okay by me!

Thanks... and we can mark this one SOLVED!

Daniel B. Martin
 
Old 01-20-2012, 04:43 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
(null)

Last edited by danielbmartin; 01-20-2012 at 07:16 PM. Reason: Answer found in Grymoire
 
  


Reply



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
sed - How do you replace end of line with a space pppaaarrrkkk Programming 7 02-07-2011 11:27 AM
sed - Problem appending at the end of line jdom Linux - Newbie 1 11-05-2010 01:21 AM
sed add a character to the end of each line keenboy Linux - General 2 08-05-2010 12:36 PM
[SOLVED] sed: How to remove the end of a line? angel115 Programming 2 10-01-2007 10:29 AM
[SOLVED] delete the end of each line in a file with sed angel115 Linux - Newbie 3 11-16-2005 04:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 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
Open Source Consulting | Domain Registration