Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm processing a text file that has a few statements of the form

'0'...'7' 

or

'0'...'9'

or even

'a'...'j'

(the idiom '...' appears only in these structures) that I'd like to convert to statements of the form...

 '0' '1' '2' '3' '4' '5' '6' '7'

I can do this with vim marcos, reasonably cutely, or knock up some python, but I'd like to do it from the command line. Can I get a recommended starting point? I'm suspecting there's some sed magic that can be done but I can't get my head around it...

share|improve this question

2 Answers 2

bash has a sequence operator that you can use:

sequence () { 
    read start finish <<< "${1/.../ }"
    start=${start//\'/}
    finish=${finish//\'/}
    eval echo {$start..$finish}
}
while read line; do
    sequence "$line"
done << END
'0'...'7' 
'0'...'9'
'a'...'j'
END
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9
a b c d e f g h i j

I have to use eval because bash will attempt to expand the sequence before it expands the variables. See http://www.gnu.org/software/bash/manual/bashref.html#Shell-Expansions

share|improve this answer

Assuming the sequences are always ascending;

$ cat input alphabet
'0'...'7'
'0'...'9'
'a'...'j'
0123456789abcdefghijklmiopqrstuvwxyzj

tr \'. '  ' <input  | 
while read f t ; do 
    grep -Po "$f.*?$t" alphabet;
done

01234567
0123456789
abcdefghij

This is probably a lot slower than the bash-eval version.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.