2

For example I just wanted to print something like this in the command line. Let say I have a file called file.txt.

 What is life?
 how are you?
 hi
 whatup
 this is more than

And I want to print it out on the command line using awk but if the number of characters is greater than 7, then the output should look like this.

 What is 
 life?
 how are 
 you?
 hi
 whatup
 this is
 more than

So basically when I use awk if the number of characters is greater than 7 it split the line into two lines on the output.

closed as unclear what you're asking by Braiam, Anthon, MelBurslan, garethTheRed, don_crissti May 12 '16 at 20:38

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    Why does your last line of desired output have more than, which is 9 characters? Shouldn't this be more and than as two separate lines? – roaima May 12 '16 at 12:47
  • 2
    does a "long" line only get split/wrapped once, or should it continue to be wrapped until it is <= 7 characters long? – Jeff Schaller May 12 '16 at 18:26
10

While you can do this in awk:

$ awk '{sub(/.{8}/,"&\n"); print}' file
What is
life?
how are
you?
hi
whatup
this is
more than

it really isn't the best tool for the job. You could do the same thing more simply with:

$ fold -sw 8 file
What is 
life?
how are 
you?
hi
whatup
this is 
more 
than

You could also use Perl:

$ perl -pe 's/.{8}/$&\n/' file
What is 
life?
how are 
you?
hi
whatup
this is 
more than
  • 1
    The fold one didn't satisfy the OP requirement.. – cuonglm May 12 '16 at 17:05
  • 1
    @cuonglm actually, the fold is the only one that is religiously satisfying the requirements, I call the example misleading – Braiam May 12 '16 at 17:32
  • 1
    @Braiam: The OP said "splitting the line into two lines", fold will split the lines into as many lines as it can. – cuonglm May 12 '16 at 17:34
  • 1
    @cuonglm and OP didn't specify if it's recursive or not. We can presume any. BTW, at least 2 out 6 solutions presented do so. – Braiam May 12 '16 at 18:36
  • @Braiam, ah. So that rule of splitting the line at most once could explain the final line being more than. – roaima May 12 '16 at 18:55
6

You could use awk, as offered in other answers, but you could also use fmt

fmt -s -w8 file
What is
life?
how are
you?
hi
whatup
this
is more
than
2

with

awk 'length < 7 { print ; next ; } 
         { printf "%s\n%s\n",substr($0,0,7),substr($0,8) }' file.txt

result is

What is
 life?
how are
 you?
hi
whatup
this is
 more than

to skip white char use

awk 'length < 7 { print ; next ; } 
    { printf "%s\n",substr($0,0,7) ; 
      i=8 ; while (substr($0,i,1) == " " ) i++; printf "%s\n",substr($0,i) }'
  • The question says "if the number of characters is greater than 7".  This answer will "split" a line that is exactly 7 characters long into one line with 7 characters and another with 0 characters. – G-Man May 18 '16 at 5:18
1

To get your desired output with sed:

$ sed -e 's/.\{7\} /&\
/' <file
What is 
life?
how are 
you?
hi
whatup
this is 
more than

Because the 8th character in your input is always space, then that did the trick.

If you want to break at 7th character regardless of 8th one:

$ sed -e 's/./\
&/8' <file
  • The question asked for a solution with awk. – user79743 May 13 '16 at 4:52
  • There is no stated rule that the 8 character will always be an space ` `. – user79743 May 13 '16 at 4:53
  • Your solution executes a hard break at the 8th character. It will fail if the a line is Why is life?, for example. – user79743 May 13 '16 at 4:54
  • The question says "if the number of characters is greater than 7". This answer (the second one) will "split" a line that is exactly 7 characters long into one line with 7 characters and another with 0 characters. – G-Man May 18 '16 at 5:17
  • @G-Man: Good point, see my updated one. – cuonglm May 19 '16 at 1:57

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