sed-less word wrap in Bash
1 Attachment(s)
Good morning,
can someone help me compose or develop a command line for a bash-script that will wrap a given expression from "$1" before a given line-limit and at a given delimiter? I have done a lot of experimenting with cut -c and -f and tr to insert line-breaks, for-loops with expr() to break the expression into lines. There have been good results, but some texts just would not be formatted correctly for my uses. The main problem is probably my abstinence from sed. So the given initial values shall be those in the following example and what I am lacking is the intelligence to make the remainder of the script run correctly. Code:
expression="$1" Otherwise, it is necessary to scroll, and sometimes a long way, to the right of the yad-dialog. I feel, that the call to the binary “wrap-tool” is a little exaggerated. C/C++ code attached. |
I think I follow, so maybe something like:
Code:
#!/usr/bin/env bash |
Quote:
All the elements are there and I do not spot a single ‘sed’. ;) But I have to admit, that it does the thing “the wrong way around”, probably because of my clumsy description, above. Do not worry, though. Your example motivates me to adapt it to my needs. Just to show you what I have failed to explain, an example string, processed “both ways”: Code:
This shall be a longer line of text, athough, ... wait, better this: "Oh but I was so much older then, I'm younger than that now" Code:
user@machine:/tmp$ ./wrap_sh "This shall be a longer line of text, although, ... wait, better this: \"Oh but I was so much older then, I'm younger than that now\"" " " 10 Code:
user@machine:/tmp$ wrap "This shall be a longer line of text, although, ... wait, better this: \"Oh but I was so much older then, I'm younger than that now\"" " " 10 |
A first working script, not yet thoroughly tested but it produces the same result as the C++-tool from the example-string in the previous post.
Code:
#!/usr/bin/env bash Probably [SOLVED] and thanks again. :) Ah. Wait. I want to mention, that an alternative approach comprised a $(expr ${#out} % $limit) and I still consider the idea worth some thinking... but alas! It caused some headache, instead. And... (edit II). Looking at this script solution, I wonder again, if I should not just stick with my wrap-tool anyway. |
awk, sed, perl or other can probably do this in a cleaner way, but the bash challenge is still a nice solution. I am not sure on the reason for a delimiter if you are going to then ignore it by saying the
limit is the overriding factor. It is probably that I do not understand the real intention of the code or if the delimiter is actually important, maybe just switch the order so length transition is performed first?? Ultimately, if you have a tool that works and no need to incorporate any other bashisms, then probably just use the tool you have :) |
Quote:
Code:
This shall be a longer line of text, although, ... wait, better this: "Oh but I was so much older then, I'm younger than that now" Code:
fold -sw40 $InFile >$OutFile Code:
This shall be a longer line of text, Daniel B. Martin |
Quote:
However, as in this way fold just replaces my own compiled utility, the “courageous shell exercise” taught me something new and was necessary. ;) This discussion results in a new blog-post. |
All times are GMT -5. The time now is 12:23 PM. |