| command/output | explanation |
$ echo {b..m}
|
Creating a range of letters. |
$ echo {7..13}
|
Creating a range of numbers. |
$ echo {7..13}\.0
|
Appending a string to each element of a range |
| $ echo {N..G}{5,0} N5 N0 M5 M0 L5 L0 K5 K0 J5 J0 I5 I0 H5 H0 G5 G0 | Forming the cartesian product of two sets (one is a range {N..G}, the other, an enumerated pair of items {5,0}) |
| $ echo {2..4}{c..a}{zoo,yam} 2czoo 2cyam 2bzoo 2byam 2azoo 2ayam 3czoo 3cyam 3bzoo 3byam 3azoo 3ayam 4czoo 4cyam 4bzoo 4byam 4azoo 4ayam $ echo sp{a,i,u}n span spin spun $ echo {ado,ba,bo,co,da,lo,overtu}r{e,n} adore adorn bare barn bore born core corn dare darn lore lorn overture overturn | Using "brace expansion" to form cartesian products and the like. |
| $ echo {{b,d,h,sc}a,fi,m{a,i,o}}{l,r}e bale bare dale dare hale hare scale scare file fire male mare mile mire mole more | Nested brace expansion. |
Slightly fancier things (and using eval)
| command/output | explanation |
$ echo {11..-4..2} |
Skipping ever other element of a range. |
$ echo {b,u,i,l,d}{b,u,i,l,d}
|
Creating a brace expression dynamically. |
$ eval echo `echo b u i l d|sed 's/^/{/;s/\ /,/g;s/$/}/'|sed 's/.*/&,&/'`
|
Using eval to expand a dynamically created brace expression |
| $ echo {a..g}{a..g}|awk '{if ($1<$2) {print $1$2}}' FS="" RS=" " ORS=" " ab ac ad ae af ag bc bd be bf bg cd ce cf cg de df dg ef eg fg | Forming the cartesian product of two sets and then removing reflexives (i~i) and symmetries (i~j = j~i). |
Some tee examples
| $seq 0 9|paste -sd+|tee /dev/tty|bc 0+1+2+3+4+5+6+7+8+9 45 | Here, we use tee to output the intermediate result, before showing the final result. |
| $ grep "^[a-z]\{7\}$" $w|shuf -n1|tee /dev/tty|sed 's/./&\ /g'|awk 'NR%2==0' RS=" " ORS=" " polymer o y e | Printing a random word followed by every other letter of that word. |
| $
echo -en {a..b}{1..5}"\n"|shuf|sed 's/^\ //g;s/^./& /g'|tee
/dev/tty|awk '!(x ~ $1) {print x,s;s=""};{ x = $1;y = $2; s=s$2 }; END
{print x,s}' b 3 b 4 b 2 a 1 a 2 b 5 a 5 a 4 a 3 b 1 b 342 a 12 b 5 a 543 b 1 | In this example, we're interested in demonstrating how awk can remember intermediate results from the prior line, in this case, we accumulate values associated with the first field, until that field changes. It's nice to have random data, which is why the shuf is used to randomize th order of the lines, but it's also nice to see the results before they're lost in the summary data output in the final steps. |