Re: how to easily strip CR from end of a string in bash?

From: Ian Giblin (giblin_at_panix.com)
Date: 08/20/03


Date: 20 Aug 2003 13:29:02 -0700


"Laurent Vogel" <lvl@club-internet.fr> wrote in message news:<3f438474$0$9625$7a628cd7@news.club-internet.fr>...
> Ian Giblin wrote:
> >
> > Please, what's the best way to strip just this CR from the end of the
> > string? If necessary we can assume that the tail of the string
> > contains an alphanumeric character before the unwanted CR.
>
> POSIX sed, one of:
> sed 's/[[:cntrl:]]$//'
> sed 's/[^[:print:]]$//'
> GNU sed:
> sed 's/\r$//'
>
> I believe Christ intended to reply to you when he wrote this in another
> thread:
> ----8<----Chris
> unix to dos:
> sed -e 's/$/\r/' unixfile.txt > dosfile.txt
>
> dos to unix:
> sed -e 's/.$//' dosfile.txt > unixfile.txt

Thanks; these work if I *know* it is a dosfile and just want to strip
the last character, but if I don't know, it may break the file. I
think I am using GNU sed here. String is a filename, typically .MP3 or
.OGG so I tried this:

ech0 "$line" | sed -e 's/[^[3Gg]]$//'

But it never gets any matches. The original version using .$ does work
but is dangerous. Is it not possible to specify a control character
like CR to sed?

Thanks, Ian.



Relevant Pages

  • Re: split() and string.whitespace
    ... wants to split on a set of characters but 'split' splits on a string, ... and others sometimes want to strip off a string but 'strip' strips on ... Too bad you haven't suggested this when they were designing Python ... could be used to form character set literals (`aeiou` => ...
    (comp.lang.python)
  • Re: varchar problem
    ... A space is just another character. ... >> want to strip them, ... VARCHAR strings will only be as long as ... > the stored string itself. ...
    (alt.php)
  • Re: ruby wish-list
    ... That only works if the character you want to strip away is the ... character in the string. ... Well you can chop off any substring with chomp. ...
    (comp.lang.ruby)
  • Stripping a string from a string
    ... I wrote a routine to strip any occurence of a character from a string (like ... Public Function oakStrip(StripString As String, ... Dim strChar As String ...
    (microsoft.public.vb.general.discussion)
  • [TOMOYO #15 3/8] Common functions for TOMOYO Linux.
    ... This file contains common functions (e.g. policy I/O, pattern matching). ... Since TOMOYO Linux is a name based access control, ... TOMOYO Linux's string manipulation functions make reviewers feel crazy, ... the Linux kernel accepts all characters but NUL character ...
    (Linux-Kernel)