wthimdh
wthimdh

Reputation: 486

how to create a list of words using seq in bash

I am using seq -s " " -f " data%g :" 5 to create

data1 : data2 : data3 : data4 : data 5 : 

However, I want it to start a number of my choice instead of 1 and not to have ":" symbol at the very end. For example something like this:

data3 : data4 : data5

Is there any one-line solution for this?

Thanks in advance!

Current output:

seq -s " " -f " data%g :" 5
data1 : data2 : data3 : data4 : data 5 :

Desired:

data3 : data4 : data5

Upvotes: 2

Views: 599

Answers (3)

Fred
Fred

Reputation: 6995

One way to do it without using an external program :

set -- {3..5} ; (($#>1)) && printf "%s:" "${@:1:$#-1 }" ; printf "%s\n" "${@: -1}"

Please note that this will overwrite positional parameters.

Upvotes: 1

karakfa
karakfa

Reputation: 67507

alternative on bash

echo data{3..5} | sed 's/ / : /g'

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Use the following approach:

start=3; seq -s" : " -f "data%g" $start 5

The output:

data3 : data4 : data5

start=3 - custom variable that contains starting index

-s" : " - item separator

$start 5 - represents the range of the sequence. In our case: from 3 to 5

Upvotes: 5

Related Questions