3

how could in bash a pattern like

ROW1   n1    n2    n3   n4 

up to 300 be done with some command in bash or also just perl...the delimiter would need to be \t

as above

4 Answers 4

9

You can use brace expansion and printf:

printf "%s\t" ROW1 n{1..300}

The first string specifies the format of output to printf, and %s is replaced with a corresponding argument. Since there is only %s, printf will re-use the format specifier until all arguments are exhausted. This will leave a trailing tab.

{1..300} is bash syntax which expands into numbers from 1 to 300, separated by spaces. If a string is added before or after the braces, the expanded form will also have that string attached.

To avoid a trailing tab, you'll have to print something separately, either the first word, or the last:

printf "ROW1"; printf "\tn%d" {1..300}
printf "%s\t" ROW1 n{1..299}; echo n300
0
3

Simpler command:

echo -n "ROW1" && echo -ne "\t"n{1..300}

Even simpler thanks to @hildred

echo -ne "ROW1" "\t"n{1..300}
1
  • 3
    even simpler: echo -ne "ROW1" "\t"n{1..300}
    – hildred
    Jun 25, 2015 at 17:29
0

In a one-liner:

echo -n "ROW1"; for ((i=1; i<=300; i++)); do echo -ne "\t n${i}"; done

Or using the same approach using brace expansion (thanks to A.B. for the suggestion):

echo -n "ROW1"; for i in {1..300}; do echo -ne "\t n${i}"; done
1
  • 1
    echo -n "ROW1"; for n in {1..300}; do echo -ne "\t n$n"; done ;)
    – A.B.
    Jun 25, 2015 at 19:36
0

Because you mentioned perl

perl -e 'printf "ROW1"; printf "\tn%d",$_ foreach (1..300)'
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .