4

I'm looking for a way to take sentences and wrap them. Using something like column is not what I want as in my scenario there will be multiple sentence columns in the 80 characters.

Is there any way to do this? I should note that doing once sentence is not an issue. It's doing multiple ones at the same time.

5/15/2014 | Friday | Server went down and cause outage | Rick accidentally shut it off.

I added the | to delimit. But each of these would be wrapped to a certain length. All at the same time with respect to each other.

  • If you're unfamiliar with the command fold, take a look at the man page, shows how to operate it, i.e. man fold. – slm May 22 '14 at 0:33
  • multiple sentence columns in the 80 characters. this part is unclear for me. – Avinash Raj May 22 '14 at 1:42
  • @AvinashRaj all of the responses so far work fine for one sentence but what if I have a lot of content that needs to be made into 5 columns. – Biff May 22 '14 at 2:29
  • 2
    @Biff IMO can you give the input and the expected output? – Avinash Raj May 22 '14 at 3:06
  • what @h.j.k has below is what I am looking for. – Biff May 22 '14 at 19:48
4

You are probably talking about fold.

$ echo abcdefghijklmnop | fold -w 4
abcd
efgh
ijkl
mnop

From man fold (showing only the most relevant options, there are a couple more):

DESCRIPTION
   Wrap  input  lines in each FILE (standard input by default), writing to
   standard output.
   -s, --spaces
          break at spaces

   -w, --width=WIDTH
          use WIDTH columns instead of 80
  • Close to what I want but I need it to also handle multiple folds at the same time. It's very hard to show an example here on SE because of formatting. But from the output of your echo, what if there were 5 or so sentences and we need 5 columns of length 4. – Biff May 22 '14 at 2:31
  • @Biff I have to second Avinash: You must provide example input and output (edit your question) to make clear what you want. – Hauke Laging May 22 '14 at 8:18
2

A simple text formatter named fmt comes with most distros. Back in the old days, before Vim's gq command, you'd run text through fmt inside of vi with: %!fmt.

It looks like there's a replacement called par but I have no experience with that.

fmt appears to be part of GNU coreutils.

1

Do you mean something like this?

$ cat temp
This is Some Long Text to Test
This is Also Some Long Text to Test
Final Call: Some Text to Complete This
$ INPUTS=""
$ FOLDWIDTH=5
$ FILENAME=temp
$ cat $FILENAME
This is Some Long Text to Test
This is Also Some Long Text to Test
Final Call: Some Text to Complete This
$ for i in $(seq 1 $(awk 'END{print NR}' $FILENAME))
> do
> INPUTS="$INPUTS <(awk 'NR==$i' $FILENAME | fold -w $FOLDWIDTH)"
> done
$ bash -c "paste $INPUTS"
This    This    Final
is So   is Al    Call
me Lo   so So   : Som
ng Te   me Lo   e Tex
xt to   ng Te   t to
 Test   xt to   Compl
         Test   ete T
                his

It's a bit hack-y though, especially the use of bash -c to start a child process to interpret the final command line. I was trying to think of other ways to parse the file line-by-line and feed each one as a file descriptor into paste but couldn't get a nice solution, thus resorting to this. Also, my suggestion is using bash process substitution in case using bash -c isn't obvious enough.

0

You could also do it in sed,

$ echo abcdefghijklmnop | sed 's/\(.\{4\}\)/\1\n/g'
abcd
efgh
ijkl
mnop

To delete the last blank line,

$ echo abcdefghijklmnop | sed 's/\(.\{4\}\)/\1\n/g' | sed '/^$/d'
abcd
efgh
ijkl
mnop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.