LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Using sed to convert a string to a character? (http://www.linuxquestions.org/questions/linux-general-1/using-sed-to-convert-a-string-to-a-character-40928/)

whansard 01-09-2003 08:25 PM

Using sed to convert a string to a character?
 
I need a command that will take a text file, and
convert carriage return, linefeed into a space,
but convert 2 consecutive carriage return linefeeds
into 1 or a single linefeed or cr.
Does anyone know sed well enough to do that for me?
i could only get sed to change one character into another,
but not change 4 characters into 2 or 1.

acid_kewpie 01-10-2003 02:34 AM

how about using tr instead?

tr 'list-of-characters-to-change' 'character-to-change-to'

whansard 01-10-2003 05:13 AM

I tried tr first. I can only get it to change one
character into another, but i couldn't get it to
change a string to a character. I was trying
stuff like the below. tr man page only says stuff
about changing single characters into single
characters.


tr '\015\012' '\012' < $1 > $1.tmp.$$

what i did yesterday for lack of a better way, was to
use tr to change the newlines and carriage returns
to Capital Z's. Then i opened a text editor and
search and replaced each instance of ZZZZ with
a ~, then search and replaced each ZZ with a space.
then i used tr and converted each ~ to a new line.
Every couple of years i end up trying to do something
like this, and can't find a good way, short of writing
a little c program, which i do so rarely, i have to
relearn every time.


All times are GMT -5. The time now is 08:45 AM.