Reputation: 1210
I have a file:
AWK question about the example
This command that works well:
awk '{ gsub(/...../, "&\n" ) ; print}' file
AWK q
uesti
on ab
out t
he ex
ample
Why this command does not print the same result?
awk '{ gsub(/.{5}/, "&\n" ) ; print}' file
AWK question about the example
Why this command does not print the same result?
awk -v WIDTH=5 '{ gsub(".{"WIDTH"}", "&\n"); print }' file
AWK question about the example
Upvotes: 2
Views: 237
Reputation: 41446
To use {5}
you need to enable re-interval
like this:
awk --re-interval '{ gsub(/.{5}/, "&\n" ) ; print}' file
awk -v WIDTH=5 --re-interval '{ gsub(".{"WIDTH"}", "&\n"); print }' file
You could also use --posix
too, but it will disable other functions in awk
awk -v WIDTH=5 --posix '{ gsub(".{"WIDTH"}", "&\n"); print }' file
Upvotes: 7
Reputation: 97918
You can use the fold
command instead of awk
:
fold -w 5 input
or if you don't have the input in a file:
echo 'AWK question about the example' | fold -w 5
Both Give:
AWK q
uesti
on ab
out t
he ex
ample
Upvotes: 5