Many a times in our scripts or otherwise we may need to print a string n times. In this article I've explored a couple of ways to do that.
Method 1: Use yes
This is the easiest one from the list. Just type yes followed by the string. That's it!
[sahil@localhost ~]#yes sometext | head -5
sometext
sometext
sometext
sometext
sometext
Method 2: Use a for loop
The for loop iterates through a defined integer value & echo the string.
[sahil@localhost ~]#for i in {1..5} ; do echo sometext ; done
sometext
sometext
sometext
sometext
sometext
Method 3: Use priintf
We can supply numbers as arguments & then use %s format specifier to pick the arguments & print them out.
[sahil@localhost ~]#printf 'sometext\n%.0s' {1..5}
sometext
sometext
sometext
sometext
sometext
Method 4: Use sequence
We can print a string n number of times using sequence. The downside is that it prints the integers too but we can 'cut' through that as shown in the example below:
[sahil@localhost ~]#seq -f "sometext%02g" 5
sometext01
sometext02
sometext03
sometext04
sometext05
[sahil@localhost ~]#seq -f "sometext%02g" 5 | cut -c1-8
sometext
sometext
sometext
sometext
sometext
Another neat little trick I found to print a string n times is to use sequence with awk. The below one liner basically populates a file with the output of the sequence command. Then the awk built in variable FILENAME uses the file name as input & prints the file name as many times as the number of lines contained in the file.
[sahil@localhost ~]#seq 1 5 > sometext ; awk '{print FILENAME}' sometext
sometext
sometext
sometext
sometext
sometext
Method 5: Use csh repeat built in:
If you have access to csh which is usually installed by default in UNIX variants, you can use its repeat built in to echo a string n times.
[sahil@localhost ~]$ repeat 5 echo sometext
sometext
sometext
sometext
sometext
sometext
[sahil@localhost ~]$
Method 1: Use yes
This is the easiest one from the list. Just type yes followed by the string. That's it!
[sahil@localhost ~]#yes sometext | head -5
sometext
sometext
sometext
sometext
sometext
Method 2: Use a for loop
The for loop iterates through a defined integer value & echo the string.
[sahil@localhost ~]#for i in {1..5} ; do echo sometext ; done
sometext
sometext
sometext
sometext
sometext
Method 3: Use priintf
We can supply numbers as arguments & then use %s format specifier to pick the arguments & print them out.
[sahil@localhost ~]#printf 'sometext\n%.0s' {1..5}
sometext
sometext
sometext
sometext
sometext
Method 4: Use sequence
We can print a string n number of times using sequence. The downside is that it prints the integers too but we can 'cut' through that as shown in the example below:
[sahil@localhost ~]#seq -f "sometext%02g" 5
sometext01
sometext02
sometext03
sometext04
sometext05
[sahil@localhost ~]#seq -f "sometext%02g" 5 | cut -c1-8
sometext
sometext
sometext
sometext
sometext
Another neat little trick I found to print a string n times is to use sequence with awk. The below one liner basically populates a file with the output of the sequence command. Then the awk built in variable FILENAME uses the file name as input & prints the file name as many times as the number of lines contained in the file.
[sahil@localhost ~]#seq 1 5 > sometext ; awk '{print FILENAME}' sometext
sometext
sometext
sometext
sometext
sometext
Method 5: Use csh repeat built in:
If you have access to csh which is usually installed by default in UNIX variants, you can use its repeat built in to echo a string n times.
[sahil@localhost ~]$ repeat 5 echo sometext
sometext
sometext
sometext
sometext
sometext
[sahil@localhost ~]$
No comments:
Post a Comment