1831614 Members
2144 Online
110027 Solutions
New Discussion

Re: 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.

Re: sed how to

the command cat filename | sed -e '/^$/d' -e 's/\(.*\),\(.*\)/\1)\2/' > newfilename appears to change ALL occurances of the , at the end of a line to a right paren. I want to change the LAST comma of the file to a right paren and preserve the remaining commas on all other lines in the file.
Roger G. Vincent
Dan Hetzel
Honored Contributor

Re: sed how to

Hi Roger,

This one works for sure:

sed -e '/^$/d' -e 's/\(,\)\([^,]*$\)/)\2/' < infile

Explanation:

/^$/d deletes empty lines
s/\(,\)\([^,]*$\)/)\2/ substitutes a comma (1st matching item) followed by 0 or more non-commas up to the end of line (2nd matching item) by a right paren followed by the 2nd match item

Matching Items are delimited by \( and \) in the search string

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com

Re: sed how to

Maybe it is me, however the last command did NOT work...It changed each line with a trailing comma to a right paren. I have the following input data:

This is first line,
This is second line,
This is the last line,

I want to leave commas on first and second line alone and change the comma on the last line to a right paren ).

Solution should be:
This is first line,
This is second line,
This is the last line)
Roger G. Vincent
Shannon Petry
Honored Contributor

Re: sed how to

You may need to use awk instead. Are all of the lines 3 long?
line1,
line2,
line3)
or is there a variety? Many files, or just one short one?
awk can use the same sed commands, but you have the ability to count, track record numbers, etc....
Microsoft. When do you want a virus today?
Rainer_1
Honored Contributor

Re: sed how to

think now it's time for awk.
try this

awk 'BEGIN{
line="";}
{
if(length($0)==0)continue;
if(length(line)>0)print line;
line=$0;
}
END{
sub(",$",")",line);
print line;
}'
Tony Constantine_1
Regular Advisor

Re: sed how to

Heres a good link for some sed 1 liners

http://www.cornerstonemag.com/sed/sed1line.txt
Kevin Ernst
Regular Advisor

Re: sed how to

As long as there aren't any blank lines at the end of the file, you got it, Ranier. I thought about using 'tac' ('cat' the file backwards) to reverse the input, search for and replace the FIRST occurrence of ",$", then 'tac' the file again to get it back in the right order--but HP-UX doesn't have a 'tac.' So much for quick-and-dirty.
Kevin Ernst
Regular Advisor

Re: sed how to

Doh! RaInEr--sorry.
Dan Hetzel
Honored Contributor

Re: sed how to

Kevin, Roger,

We don't have 'tac' but we have 'rev' which prints every line backwards

one solution could be:

rev infile | sed -e '/^$/d -e '$s/,/)/' | rev > outputfile

or

sed -e '/^$/d' < infile | sed -e '$s/\(,\)\([^,]*$\)/)\2/'

will both replace the last comma of the last non blank line by a right paren, even if it's not the last character on the line,providing there is a comma on that line.


Dan



Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com