1752786 Members
6107 Online
108789 Solutions
New Discussion юеВ

sed how to

 
SOLVED
Go to solution

sed how to

I need to modify a file in the following manner:
- remove all blank lines
- change the last occurance of a comma (,) to a left paren

any ideas
Roger G. Vincent
18 REPLIES 18
Lasse Knudsen
Esteemed Contributor
Solution

Re: sed how to

cat file | grep -v '^$' | sed -e 's/,$/(/'
In a world without fences - who needs Gates ?
Rainer_1
Honored Contributor

Re: sed how to

sed -e '/^$/d' -e 's/,$/(/'
Bruce Regittko
New Member

Re: sed how to

Hi,

Is the comma the last character of the line? If so, then the first two suggestions will work fine. If you mean the last comma, but not necessarily the last character, try the following:

sed -e '/^$/d' -e 's/,[^,]*$/(/'

Hope this helps,

Bruce
Lasse Knudsen
Esteemed Contributor

Re: sed how to

Roger, the other 2 posts definetely desirve 10pts for their smooth and "true hack" solutions.
In a world without fences - who needs Gates ?

Re: sed how to

This was close however, I want to change the last occurance of a comma (,) in the file to a paren NOT each occurance of a comma to a paren.
Roger G. Vincent
John Palmer
Honored Contributor

Re: sed how to

Hi,

To change the last occurrence of ',' try the following:-

sed -e '/^$/d' -e 's/\(.*\),\(.*\)/\1(\2/'

Hope the backslashes appear! there's enough of them.

Regards,
John
CHRIS ANORUO
Honored Contributor

Re: sed how to

Try: cat file |sed -e '/^$/d' -e 's/\(.*\),\(.*\)/\1(\2/' > newfile


When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.

Re: sed how to

The commas are killing me. I have a file with multiple lines which end with a comma (,). I need to change the very last comma of the file to a right paren ")". I am sorry for the confusion, however, I stated the question improperly (or at least it was unclear). I am new to this point value stuff and I appreciate all responses and will get the points right soon.
Roger G. Vincent
CHRIS ANORUO
Honored Contributor

Re: sed how to

Hi Rogers,

Check this very well and run into another file as indicated: cat filename |sed -e '/^$/d' -e 's/\(.*\),\(.*\)/\1)\2/' > newfilename
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.