5

I would like to have the following sequence: OW HW1 HW2 being repeated in an output file 3000 times such as:

OW
HW1
HW2
.
.
.
OW
HW1
HW2

I believe I can, by using bash, set the OW, HW1 and HW2 as independent variable and then make a do loop 3000 times and print the values in the output file.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

7 Answers7

13

You can use this printf trick with a zero-length string specifier %.0s and brace expansion:

printf '%.0sOW\nHW1\nHW2\n' {1..3000} > newfile
steeldriver
  • 81,074
7

If use for in cycle:

for i in $(seq 1 3000) ; do echo -e "OW\nHW1\nHW2"; done

or use echo 3x

for i in $(seq 1 3000) ; do echo OW; echo HW1; echo HW2; done
schweik
  • 1,250
7

It can be easily done with Perl:

perl -e'print "OW\n\HW1\nHW2\n"x3000'
4

You could simply use yes and head to generate this sequence 3000 times.

yes $'OW\nHW1\nHW2' | head -9000

Here you need the 9000 because you have 3 lines and you want those 3 lines 3000 times.

Gounou
  • 549
  • 3
  • 5
HSchmale
  • 427
3

If your shell offers "brace expansion", try

echo $'\nOW\nHW1\nHW2\n'{1..3000}$'\n' | grep -Ev "^( |[0-9])*$"
OW
HW1
HW2
OW
HW1
HW2
OW
HW1
HW2
.
.
.

and redirect to a file if happy with the result.

Gounou
  • 549
  • 3
  • 5
RudiC
  • 8,969
0

you can use a while loop like here: http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_02.html

An example:

COUNTER=0; while [ $COUNTER -lt 3 ]; do echo "$COUNTER" && COUNTER=$((COUNTER+1)); done

Don't forget to reset the COUNTER every time you want to run this.

Timo
  • 101
  • 2
0

Python3 solution

python3 -c 'print("OW\nHW1\nHW2\n"*3000, end="")' 
iruvar
  • 16,725