LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-09-2003, 08:25 PM   #1
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,304

Rep: Reputation: 65
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.
 
Old 01-10-2003, 02:34 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
how about using tr instead?

tr 'list-of-characters-to-change' 'character-to-change-to'
 
Old 01-10-2003, 05:13 AM   #3
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,304

Original Poster
Rep: Reputation: 65
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.

Last edited by whansard; 01-10-2003 at 05:55 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove last character from file/string linuxchump Programming 34 06-08-2009 04:01 AM
Picking a character from a string randomly swatward Programming 2 08-14-2005 01:21 AM
insert string with sed greg108 Programming 7 02-18-2005 01:11 PM
how to print the first character in a string using strtok its_godzilla Programming 5 02-02-2005 10:22 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration