4

How do I use awk to repeat a block of text 100 times?

ie

string1
string2
string3

Would become:

string1
string2
string3
string1
string2
string3
string1
string2
string3...
  • 1
    Do you need to repeat only a section of your input or the whole input ? – don_crissti Jul 18 '16 at 21:09
3

(where input is a file containing the text to repeat)

With GNU awk (and assuming the input doesn't contain NUL bytes):

awk -v n=100 -v RS='\0' -v ORS= '{while (n--) print}' < input

Portably:

awk -v n=100 -v ORS= '
  {all = all $0 RS}
  END {while (n--) print all}' < input

(note that if the input didn't end with a newline character, one will be added).

You can also do things like:

set --
n=100; while [ "$n" -gt 0 ]; do
  set -- "$@" input
  n=$((n - 1))
done
cat "$@"

That one will work regardless of what byte input contains. Note however, that for larger values of n, and if your shell doesn't have cat builtin you may reach the system limit on the size of the arguments to a command (here cat).

With zsh or recent versions of bash or ksh93 or yash -o braceexpand, for a fixed text you can do things like:

printf '%.0sstring1\nstring2\nstring3\n' {1..100}

With zsh:

repeat 100 print -l string{1..3}

If we're being cheeky, on systems with GNU seq installed, we can also do:

seq -f 'string1
string2
string%.0g' 3.0001 0.0001 3.0100
| improve this answer | |
  • seq -f .. how to work?! and why use 3.0001 to 3.0100 !?! why not use 1 to 100 ?!?! thankyou – Baba Jul 19 '16 at 20:25
4

Although this question is tagged awk, I would like to add a simple solution that does not use awk:

seq 100 | xargs -n1 -I{} echo "1 2 3"

If the data you with to repeat is in a file, use this instead:

seq 100 | xargs -n1 -I{} cat filename

Another solution might use the command yes, which simply repeats its arguments:

yes "1 2 3" | head -n100
| improve this answer | |
  • Note that it runs one echo/cat command per iteration. – Stéphane Chazelas Jul 19 '16 at 10:42
  • @StéphaneChazelas I know. But I like the functional approach. – Michael Vehrs Jul 19 '16 at 11:37
  • I think we can safely assume "a block of text" consists of several lines... – don_crissti Jul 19 '16 at 13:14
  • @don_crissti So what? You can use "$(echo -e whatever)" or "$(<input)" as an argument to yes. – Michael Vehrs Jul 19 '16 at 13:31
  • 2
    @MichaelVehrs, yes though you'd need to adapt the number passed to head -n based on the number of lines in the input (like head -n 300 for a 3-line input). Also note that $(<input) strips all trailing newlines from the content of input (yes will add one back). – Stéphane Chazelas Jul 19 '16 at 13:39
3

With awk:

echo "t1 t2 t3" | awk '{l=$0; for(i=1;i++<100;) l=l" "$0} END{print l}'

AND with perl:

echo "t1 t2 t3" | perl -ne 'chomp;print "$_ " x 100;'
| improve this answer | |
1

Instead of using AWK I would simply use a for-loop:

for i in $(seq 100); do
    echo string1
    echo string2
    echo string3
done
| improve this answer | |
  • That's running 301 commands (one of which non-standard) instead of one awk one. – Stéphane Chazelas Jul 19 '16 at 10:41
  • @StéphaneChazelas echo is a shell builtin, so it does not have the overhead of an external command. – August Karlstrom Jul 19 '16 at 16:51
0

On /bin/bash, you can parameterize your printed records, so you can print any three values among your file records, n times in a row. They do not have to be consecutive.

Say you want to print out records 2, 3 and 6 from your input file: inputfile

$ cat inputfile
A
B
C
D
E
F
$ awk -v RS='\n' '{if(NR=="2"||NR=="3"||NR=="6") a[NR]=$0} \
END{for(i=1;i++<=100;) {print a[2]"\n"a[3]"\n"a[6];}}' inputfile

Arguably this costs a little more because an array is invoked.

| improve this answer | |
  • Provided as a reaction to @don_crissti's comment ... – Cbhihe Jul 19 '16 at 14:25
  • You misread my comment though maybe it's my fault as I wasn't clear enough... Obviously, extracting a section of text and repeating it is as simple as repeating the whole input...What I meant was "repeat only a section of the input while leaving the rest of the input intact..." – don_crissti Jul 19 '16 at 17:23
  • @don_crissti: I now got what you meant. It was indeed not clear from OP's phrasing... Different but still feasible in more than one way. Good homework for class. – Cbhihe Jul 19 '16 at 19:27

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.