Domanda

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
È stato utile?

Soluzione

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

Altri suggerimenti

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top