LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Tags used in this thread
Popular LQ Tags , , ,

Reply
 
Thread Tools Search this Thread
Old 05-05-2007, 10:11 AM   #1
fsenes
LQ Newbie
 
Registered: May 2007
Posts: 2
Thanked: 0
Simulating Word Wrap with "probably" SED


[Log in to get rid of this advertisement]
Hi
I need a SED line to insert \n (newline) after nth character of a line.

My goal is to simulate word wrap.

Suppose that I have a line like this:

You should include as much detail as possible in your message, including exact error messages (where applicable) and what you have done so far.

I want it as follows:

You should include as much detail as possible (\n)
in your message, including exact error messages (\n)
(where applicable) and what you have done so far. (\n)

In this example the sum of characters for each line is 50

Appreciate very much any help
Best

Last edited by fsenes; 05-05-2007 at 10:13 AM..
fsenes is offline  
Tag This Post , , ,
Reply With Quote
Old 05-05-2007, 12:43 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,058
Thanked: 1
Maybe something like this...
Code:
sed -e 's/.\{45\} /&\n/g' file.txt
homey is offline     Reply With Quote
Old 05-06-2007, 01:11 AM   #3
fsenes
LQ Newbie
 
Registered: May 2007
Posts: 2
Thanked: 0

Original Poster
Quote:
Originally Posted by homey
Maybe something like this...
Code:
sed -e 's/.\{45\} /&\n/g' file.txt
Worked near to perfect
But may not work if the word containing the 45th chr is very long. Therefore counting 50 chars than returning to the last occurance of "SPACE" or;
replacing the occurance closest (but before) to 50th chr of "SPACE" with "NEWLINE" may handle the problem better I think.

Thks anyway indeed

Last edited by fsenes; 05-06-2007 at 02:23 AM..
fsenes is offline     Reply With Quote
Old 03-02-2009, 07:50 PM   #4
A.Thyssen
LQ Newbie
 
Registered: May 2006
Posts: 12
Thanked: 0
Lightbulb

Quote:
Originally Posted by homey View Post
Maybe something like this...
Code:
sed -e 's/.\{45\} /&\n/g' file.txt
That is very near perfect for a sed text word-wrap formater..

However \n does not work on all sed's and it will stuff up for input already with some extra spacing and line formating.

here is my final solution...

For sanitized text (like a column of words, as in the output of a piped 'ls')
Code:
tr '\012' ' ' |
  sed 's/ $/@/; s/[^@]\{55\} /&@/g' |
    tr '@' '\012'
feed the output of 'ls' to it to test...

For unsanatized blank line separated paragraphs,
Code:
tr -s ' ' ' ' | tr '\012' ' ' |
  sed 's/ $/@/; s/   */@@/g; s/[^@]\{55\} /&@/g' |
    tr '@' '\012'
try feeding a 'ls -C' output to this!!!

The sanatization can still be improved... any takers?
A.Thyssen is offline     Reply With Quote
Old 04-15-2009, 05:38 PM   #5
gmendoza
LQ Newbie
 
Registered: Nov 2007
Distribution: Ubuntu, Debian, Slackware
Posts: 6
Thanked: 2
Cool

Quote:
Originally Posted by fsenes View Post
My goal is to simulate word wrap.

Suppose that I have a line like this:

You should include as much detail as possible in your message, including exact error messages (where applicable) and what you have done so far.

I want it as follows:

You should include as much detail as possible (\n)
in your message, including exact error messages (\n)
(where applicable) and what you have done so far. (\n)

In this example the sum of characters for each line is 50

Appreciate very much any help
Best
You may be interested in the "fold" command, which is included with almost any GNU system.

Code:
fold -s -w 50 textfile.txt
This would wrap at 50 characters, but only at spaces, so as not to chop words in the middle.

See "man fold" for more info.



Regards

Gilbert

Last edited by Tinkster; 04-15-2009 at 05:51 PM.. Reason: [mod_edit]shameless plug removed ;)
gmendoza is offline     Reply With Quote
Old 04-16-2009, 12:24 AM   #6
A.Thyssen
LQ Newbie
 
Registered: May 2006
Posts: 12
Thanked: 0
The "fold" command is good, but it only breaks long lines.

If you are going to use a linux command, the better one is "fmt" which has options to fill-out short lines, specify margins, and prefix strings, and also remove extra spaces between words.

Basically it can do what "fold" does, and more.


Another similar program that does not seem to have gained the popularity it deserves is "par"
http://en.wikipedia.org/wiki/Par_(command)
http://www.nicemice.net/par/

The nice thing about "par" is it understands and preserves things like C-comments, and Mail Quotation indenting characters.

Unfortunately it isn't readily available in linux repositories, and its documentation is bad, though the command itself is simple.
A.Thyssen is offline     Reply With Quote
Old 04-16-2009, 01:43 AM   #7
gmendoza
LQ Newbie
 
Registered: Nov 2007
Distribution: Ubuntu, Debian, Slackware
Posts: 6
Thanked: 2
Quote:
Originally Posted by A.Thyssen View Post
The "fold" command is good, but it only breaks long lines.

If you are going to use a linux command, the better one is "fmt" which has options to fill-out short lines, specify margins, and prefix strings, and also remove extra spaces between words.

Basically it can do what "fold" does, and more.
Oooo... now fmt is cool. I really like the filling out of short lines, and uniformed spacing. Interestingly, it won't add an extra space between sentences that only have one space, however it does remove extra spaces between sentences nicely. I can think of an easy sed command to fix the former, anyway. Great suggestion, thanks!

Quote:
Originally Posted by A.Thyssen View Post
Another similar program that does not seem to have gained the popularity it deserves is "par"
http://en.wikipedia.org/wiki/Par_(command)
http://www.nicemice.net/par/
I came across par a while back, and it appears the documentation hasn't improved at all since then.
gmendoza is offline     Reply With Quote

Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using sed to remove word wrap Harpune Linux - Software 2 03-02-2009 07:53 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 3 07-24-2007 12:03 PM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 11:47 AM
"enscript --word-wrap" does not wrap line of text file powah Linux - General 3 05-16-2006 10:12 PM


All times are GMT -5. The time now is 03:27 PM.

Main Menu
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.
Advertisement
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Click Here to receive a complimentary subscription courtesy of LQ.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration