Simple sed question

Simple sed question

Post by Sank » Wed, 19 Mar 2003 23:40:20



I have a file containing lines like this:

include/SessionDefs.h,                             1.118,,,
src/screens/apply_joint.trn,                1.7,
src/screens/custapp_appn_review.obj,        1.30,,,,,,,

I would like to get rid of all the trailing commas after the number.
Otherwise,the tabs etc all can stay. Please notr that I have to keep
the comma after the text.

Is there a sed command that would do this? any advice is appreciated.

 
 
 

Simple sed question

Post by Chris Coyl » Wed, 19 Mar 2003 23:59:23



> I have a file containing lines like this:

> include/SessionDefs.h,                             1.118,,,
> src/screens/apply_joint.trn,                1.7,
> src/screens/custapp_appn_review.obj,        1.30,,,,,,,

> I would like to get rid of all the trailing commas after the number.
> Otherwise,the tabs etc all can stay. Please notr that I have to keep
> the comma after the text.

> Is there a sed command that would do this? any advice is appreciated.

The way to think about this is this: you need to match a digit followed by
any number of commas, and replace with the original matching digit.
To match the pattern you need is '[0-9],*'
To use the matched digit in the replacement, you need to mark it,
like this: '\([0-9]\),*'.  Then you can use '\1' in the replacement
to get back the digit which matched.
Putting this together, your sed command should be:

sed -e 's/\([0-9]\),*/\1/'

If the pattern you want to replace may occur more than once per line,
then you need the 'g' suffix:

sed -e 's/\([0-9]\),*/\1/g'

 
 
 

Simple sed question

Post by Daniele Di Matti » Wed, 19 Mar 2003 23:56:05



Quote:> I have a file containing lines like this:

> include/SessionDefs.h,                             1.118,,,
> src/screens/apply_joint.trn,                1.7,
> src/screens/custapp_appn_review.obj,        1.30,,,,,,,

> I would like to get rid of all the trailing commas after the number.
> Otherwise,the tabs etc all can stay. Please notr that I have to keep
> the comma after the text.

> Is there a sed command that would do this? any advice is appreciated.

daniele> cat data
include/SessionDefs.h,                             1.118,,,
src/screens/apply_joint.trn,                1.7,
src/screens/custapp_appn_review.obj,        1.30,,,,,,,

daniele> sed 's/,\{1,\}$//' data
include/SessionDefs.h,                             1.118
src/screens/apply_joint.trn,                1.7
src/screens/custapp_appn_review.obj,        1.30

daniele>

hi!

 
 
 

Simple sed question

Post by William Par » Thu, 20 Mar 2003 00:24:33



> I have a file containing lines like this:

> include/SessionDefs.h,                             1.118,,,
> src/screens/apply_joint.trn,                1.7,
> src/screens/custapp_appn_review.obj,        1.30,,,,,,,

> I would like to get rid of all the trailing commas after the number.
> Otherwise,the tabs etc all can stay. Please notr that I have to keep
> the comma after the text.

> Is there a sed command that would do this? any advice is appreciated.

Key insight: ',*$'

--

Linux solution for data management and processing.

 
 
 

Simple sed question

Post by David Thompso » Thu, 20 Mar 2003 01:07:55




> > I have a file containing lines like this:

> > include/SessionDefs.h,                             1.118,,,
> > src/screens/apply_joint.trn,                1.7,
> > src/screens/custapp_appn_review.obj,        1.30,,,,,,,

> > I would like to get rid of all the trailing commas after the number.
> > Otherwise,the tabs etc all can stay. Please notr that I have to keep
> > the comma after the text.

> > Is there a sed command that would do this? any advice is appreciated.

> Key insight: ',*$'

Yes, so simple I wonder why you didn't just say it,

  sed 's/,*$//' filename

--
David Thompson

 
 
 

Simple sed question

Post by William Par » Thu, 20 Mar 2003 01:14:04



>> Key insight: ',*$'

> Yes, so simple I wonder why you didn't just say it,

>  sed 's/,*$//' filename

Because I'm tired of doing other people's homework.  The best anyone can
do is to point them in the right direction.  They have to do the
walking, I'm afraid.

--

Linux solution for data management and processing.

 
 
 

Simple sed question

Post by laura fairhe » Thu, 20 Mar 2003 01:28:22



Quote:>I have a file containing lines like this:

>include/SessionDefs.h,                             1.118,,,
>src/screens/apply_joint.trn,                1.7,
>src/screens/custapp_appn_review.obj,        1.30,,,,,,,

>I would like to get rid of all the trailing commas after the number.
>Otherwise,the tabs etc all can stay. Please notr that I have to keep
>the comma after the text.

>Is there a sed command that would do this? any advice is appreciated.

Hi Sankar,

You can do this in 'sed' with a single 's' substitute command,

s/,*$//

,*   match 0 or more comma characters
$    followed by the end of the line

You could look for 1 or more comma characters but it makes the expression
bulkier;

s/,\{1,\}$//

of course there is no difference because the original code replaces sometimes
nothing at the end of a line (0 comma characters) with nothin' which had no
effect ( possibly the first command is a little slower thou`)

( Both of these substitutions assume that simply removing any trailing commas
  from a line is good enough if not just extend the expression to suit )

btw: You can find complete documentation for 'sed' from the original authors
on my web-site, here;

http://lf.1accesshost.com/UNIX/UNIX.HTM

byefornow
laura

--
alt.fan.madonna |news, interviews, discussion, writings
                |chat, exchange merchandise, meet fans....
                |Get into the groove baby you've got to... check us out!

 
 
 

Simple sed question

Post by Bill Marcu » Thu, 20 Mar 2003 09:58:51


On 18 Mar 2003 06:40:20 -0800, Sankar

> I have a file containing lines like this:

> include/SessionDefs.h,                             1.118,,,
> src/screens/apply_joint.trn,                1.7,
> src/screens/custapp_appn_review.obj,        1.30,,,,,,,

> I would like to get rid of all the trailing commas after the number.
> Otherwise,the tabs etc all can stay. Please notr that I have to keep
> the comma after the text.

> Is there a sed command that would do this? any advice is appreciated.

sed 's/,*$//'

--
Ever wonder if taxation without representation might have been cheaper?