Apply brace expansion in “reverse order”Renaming a file to a shorter name easily using something like brace expansionBrace compressionBash Brace Expansion & VariablesExpand variable in brace expansionRight-to-left shell brace expansionPerform parameter expansion before brace expansion?bash combining wildcard expansion with brace expansionbackticks inside brace expansionNested brace expansion mystery in Bashbash in-line brace expansion
Suggested order for Amazon Prime Doctor Who series
Parameterize chained calls to a utility program in Bash
How does the spell Remove Curse interact with a Sword of Vengeance?
Helping ease my back pain when I'm studying 13 hours everyday, even weekends
Can there be an UN resolution to remove a country from the UNSC?
What did River say when she woke from her proto-comatose state?
Who are the remaining King/Queenslayers?
What exactly is the 'online' in OLAP and OLTP?
How can I politely work my way around not liking coffee or beer when it comes to professional networking?
Dates on degrees don’t make sense – will people care?
If I wouldn't want to read the story, is writing it still a good idea?
Can any NP-Complete Problem be solved using at most polynomial space (but while using exponential time?)
How to model a twisted cylinder like this
How is hair tissue mineral analysis performed?
Does this Wild Magic result affect the sorcerer or just other creatures?
Would it be a copyright violation if I made a character’s full name refer to a song?
Why do some professors with PhDs leave their professorships to teach high school?
Old sci-fi story: radiation mutated the animals, a boy loses a limb, but it's okay because "humans used to do great with only two arms"
Can humans ever directly see a few photons at a time? Can a human see a single photon?
How does a blind passenger not die, if driver becomes unconscious
JSON selector class in Python
What is the origin of Scooby-Doo's name?
Employer wants to use my work email account after I quit
Count All Possible Unique Combinations of Letters in a Word
Apply brace expansion in “reverse order”
Renaming a file to a shorter name easily using something like brace expansionBrace compressionBash Brace Expansion & VariablesExpand variable in brace expansionRight-to-left shell brace expansionPerform parameter expansion before brace expansion?bash combining wildcard expansion with brace expansionbackticks inside brace expansionNested brace expansion mystery in Bashbash in-line brace expansion
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
For example a..b1..3
expands to a1 a2 a3 b1 b2 b3
.
If I wanted to print a1 b1 c1 a2 b2 c2 a3 b3 c3
, is there an analogous way to do that? What's the simplest way?
bash brace-expansion
add a comment |
For example a..b1..3
expands to a1 a2 a3 b1 b2 b3
.
If I wanted to print a1 b1 c1 a2 b2 c2 a3 b3 c3
, is there an analogous way to do that? What's the simplest way?
bash brace-expansion
add a comment |
For example a..b1..3
expands to a1 a2 a3 b1 b2 b3
.
If I wanted to print a1 b1 c1 a2 b2 c2 a3 b3 c3
, is there an analogous way to do that? What's the simplest way?
bash brace-expansion
For example a..b1..3
expands to a1 a2 a3 b1 b2 b3
.
If I wanted to print a1 b1 c1 a2 b2 c2 a3 b3 c3
, is there an analogous way to do that? What's the simplest way?
bash brace-expansion
bash brace-expansion
edited 8 hours ago
K7AAY
2,08011029
2,08011029
asked 8 hours ago
RUBEN GONÇALO MOROUÇORUBEN GONÇALO MOROUÇO
434
434
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
You could do:
$ eval echo 'a..c'1..3
a1 b1 c1 a2 b2 c2 a3 b3 c3
Which then tells the shell to evaluate:
echo a..c1 a..c2 a..c3
add a comment |
a..c1 a..c2 a..c3
The brace expansions in a..c1..3
are expanded left to right, so you first get a1..3 b1..3 c1..3
and then the letters are combined with the numbers into a1 a2 a3 b1 b2 b3 c1 c2 c3
. To get the order you want, you will have to use the slightly longer expression above.
If you wanted to do it for a large range of "numbers" it wouldn't be practical anymore.
– RUBEN GONÇALO MOROUÇO
8 hours ago
1
@RUBENGONÇALOMOROUÇO No it wouldn't, and if you are doing it for a large range of numbers, I would suggest using an alternative approach, like a double loop. That would work for many thousands of combinations, whereas a brace expansions my trigger "argument list too long" in certain contexts.
– Kusalananda♦
8 hours ago
add a comment |
For this particular case, I think that the option given by Stéphane Chazelas is the best one.
On the other hand, when you expand more complex things, this option doesn't scale well. So, you can achieve the same with this:
printf '%s' a..c1..3 | sort -zk 1.2,1.2 | tr '' ' '
which returns:
a1 b1 c1 a2 b2 c2 a3 b3 c3
Seems a little messy, but now, I have a huge control in the order, just changing two chars in the command above; for example:
echo a..b1..2a..b1..2
this will expand to:
a1a1 a1a2 a1b1 a1b2 a2a1 a2a2 a2b1 a2b2 b1a1 b1a2 b1b1 b1b2 b2a1 b2a2 b2b1 b2b2
Suppose I want all the 1
in the second expansion, then the 2
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.2,1.2 | tr '' ' '
a1a1 a1a2 a1b1 a1b2 b1a1 b1a2 b1b1 b1b2 a2a1 a2a2 a2b1 a2b2 b2a1 b2a2 b2b1 b2b2
Suppose I want all the a
in the third expansion, then the b
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.3,1.3 | tr '' ' '
a1a1 a1a2 a2a1 a2a2 b1a1 b1a2 b2a1 b2a2 a1b1 a1b2 a2b1 a2b2 b1b1 b1b2 b2b1 b2b2
Suppose I want all the 1
in the fourth expansion, then the 2
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.4,1.4 | tr '' ' '
a1a1 a1b1 a2a1 a2b1 b1a1 b1b1 b2a1 b2b1 a1a2 a1b2 a2a2 a2b2 b1a2 b1b2 b2a2 b2b2
Suppose I want all the 1a
in the middle, then 1b
, then 2a
, then 2b
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.2,1.3 | tr '' ' '
a1a1 a1a2 b1a1 b1a2 a1b1 a1b2 b1b1 b1b2 a2a1 a2a2 b2a1 b2a2 a2b1 a2b2 b2b1 b2b2
You can even, just as easily, reverse any order in the expansions above, just adding and r
to the previous command; for example, the last one:
printf '%s' a..b1..2a..b1..2 | sort -rzk 1.2,1.3 | tr '' ' '
b2b2 b2b1 a2b2 a2b1 b2a2 b2a1 a2a2 a2a1 b1b2 b1b1 a1b2 a1b1 b1a2 b1a1 a1a2 a1a1
add a comment |
Using a loop:
for l in a..b; do printf '%sn' "$l"1..3; done
This will loop through your first expansion and then expand each character with the second.
If you need the output all on one line you can remove the n
:
for l in a..b; do printf '%s ' "$l"1..3; done
This won't give you a trailing newline but if you are passing it to a command or variable that shouldn't be an issue.
add a comment |
bash, ksh, zsh
A one liner that works in (bash, ksh, zsh) (not all shells can do "Brace expansion" in reverse order):
$ echo 3..1c..a | rev
a1 b1 c1 a2 b2 c2 a3 b3 c3
An alternative that use eval
(which still is for bash,ksh,zsh and may be more criptic) is:
$ eval echo 'a..c'1..3
a1 b1 c1 a2 b2 c2 a3 b3 c3
To understand what happens, replace eval
with echo
:
$ echo echo 'a..c'1..3
echo a..c1 a..c2 a..c3
The command executed (after eval expansion) is actually echo a..c1 a..c2 a..c3
. Which expands as you want/need.
all shells
For all shells, use:
$ for i in 1 2 3; do for j in a b c; do printf "%s%s " "$j" "$i"; done; done; echo
a1 b1 c1 a2 b2 c2 a3 b3 c3
If you don't mind a trailing space.
If you must have no trailing space added:
$ s=""; for i in 1 2 3; do for j in a b c; do printf "%s%s" "$s" "$j" "$i"; s=" "; done; done; echo
a1 b1 c1 a2 b2 c2 a3 b3 c3
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f525921%2fapply-brace-expansion-in-reverse-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could do:
$ eval echo 'a..c'1..3
a1 b1 c1 a2 b2 c2 a3 b3 c3
Which then tells the shell to evaluate:
echo a..c1 a..c2 a..c3
add a comment |
You could do:
$ eval echo 'a..c'1..3
a1 b1 c1 a2 b2 c2 a3 b3 c3
Which then tells the shell to evaluate:
echo a..c1 a..c2 a..c3
add a comment |
You could do:
$ eval echo 'a..c'1..3
a1 b1 c1 a2 b2 c2 a3 b3 c3
Which then tells the shell to evaluate:
echo a..c1 a..c2 a..c3
You could do:
$ eval echo 'a..c'1..3
a1 b1 c1 a2 b2 c2 a3 b3 c3
Which then tells the shell to evaluate:
echo a..c1 a..c2 a..c3
answered 8 hours ago
Stéphane ChazelasStéphane Chazelas
322k57622987
322k57622987
add a comment |
add a comment |
a..c1 a..c2 a..c3
The brace expansions in a..c1..3
are expanded left to right, so you first get a1..3 b1..3 c1..3
and then the letters are combined with the numbers into a1 a2 a3 b1 b2 b3 c1 c2 c3
. To get the order you want, you will have to use the slightly longer expression above.
If you wanted to do it for a large range of "numbers" it wouldn't be practical anymore.
– RUBEN GONÇALO MOROUÇO
8 hours ago
1
@RUBENGONÇALOMOROUÇO No it wouldn't, and if you are doing it for a large range of numbers, I would suggest using an alternative approach, like a double loop. That would work for many thousands of combinations, whereas a brace expansions my trigger "argument list too long" in certain contexts.
– Kusalananda♦
8 hours ago
add a comment |
a..c1 a..c2 a..c3
The brace expansions in a..c1..3
are expanded left to right, so you first get a1..3 b1..3 c1..3
and then the letters are combined with the numbers into a1 a2 a3 b1 b2 b3 c1 c2 c3
. To get the order you want, you will have to use the slightly longer expression above.
If you wanted to do it for a large range of "numbers" it wouldn't be practical anymore.
– RUBEN GONÇALO MOROUÇO
8 hours ago
1
@RUBENGONÇALOMOROUÇO No it wouldn't, and if you are doing it for a large range of numbers, I would suggest using an alternative approach, like a double loop. That would work for many thousands of combinations, whereas a brace expansions my trigger "argument list too long" in certain contexts.
– Kusalananda♦
8 hours ago
add a comment |
a..c1 a..c2 a..c3
The brace expansions in a..c1..3
are expanded left to right, so you first get a1..3 b1..3 c1..3
and then the letters are combined with the numbers into a1 a2 a3 b1 b2 b3 c1 c2 c3
. To get the order you want, you will have to use the slightly longer expression above.
a..c1 a..c2 a..c3
The brace expansions in a..c1..3
are expanded left to right, so you first get a1..3 b1..3 c1..3
and then the letters are combined with the numbers into a1 a2 a3 b1 b2 b3 c1 c2 c3
. To get the order you want, you will have to use the slightly longer expression above.
answered 8 hours ago
Kusalananda♦Kusalananda
152k18298478
152k18298478
If you wanted to do it for a large range of "numbers" it wouldn't be practical anymore.
– RUBEN GONÇALO MOROUÇO
8 hours ago
1
@RUBENGONÇALOMOROUÇO No it wouldn't, and if you are doing it for a large range of numbers, I would suggest using an alternative approach, like a double loop. That would work for many thousands of combinations, whereas a brace expansions my trigger "argument list too long" in certain contexts.
– Kusalananda♦
8 hours ago
add a comment |
If you wanted to do it for a large range of "numbers" it wouldn't be practical anymore.
– RUBEN GONÇALO MOROUÇO
8 hours ago
1
@RUBENGONÇALOMOROUÇO No it wouldn't, and if you are doing it for a large range of numbers, I would suggest using an alternative approach, like a double loop. That would work for many thousands of combinations, whereas a brace expansions my trigger "argument list too long" in certain contexts.
– Kusalananda♦
8 hours ago
If you wanted to do it for a large range of "numbers" it wouldn't be practical anymore.
– RUBEN GONÇALO MOROUÇO
8 hours ago
If you wanted to do it for a large range of "numbers" it wouldn't be practical anymore.
– RUBEN GONÇALO MOROUÇO
8 hours ago
1
1
@RUBENGONÇALOMOROUÇO No it wouldn't, and if you are doing it for a large range of numbers, I would suggest using an alternative approach, like a double loop. That would work for many thousands of combinations, whereas a brace expansions my trigger "argument list too long" in certain contexts.
– Kusalananda♦
8 hours ago
@RUBENGONÇALOMOROUÇO No it wouldn't, and if you are doing it for a large range of numbers, I would suggest using an alternative approach, like a double loop. That would work for many thousands of combinations, whereas a brace expansions my trigger "argument list too long" in certain contexts.
– Kusalananda♦
8 hours ago
add a comment |
For this particular case, I think that the option given by Stéphane Chazelas is the best one.
On the other hand, when you expand more complex things, this option doesn't scale well. So, you can achieve the same with this:
printf '%s' a..c1..3 | sort -zk 1.2,1.2 | tr '' ' '
which returns:
a1 b1 c1 a2 b2 c2 a3 b3 c3
Seems a little messy, but now, I have a huge control in the order, just changing two chars in the command above; for example:
echo a..b1..2a..b1..2
this will expand to:
a1a1 a1a2 a1b1 a1b2 a2a1 a2a2 a2b1 a2b2 b1a1 b1a2 b1b1 b1b2 b2a1 b2a2 b2b1 b2b2
Suppose I want all the 1
in the second expansion, then the 2
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.2,1.2 | tr '' ' '
a1a1 a1a2 a1b1 a1b2 b1a1 b1a2 b1b1 b1b2 a2a1 a2a2 a2b1 a2b2 b2a1 b2a2 b2b1 b2b2
Suppose I want all the a
in the third expansion, then the b
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.3,1.3 | tr '' ' '
a1a1 a1a2 a2a1 a2a2 b1a1 b1a2 b2a1 b2a2 a1b1 a1b2 a2b1 a2b2 b1b1 b1b2 b2b1 b2b2
Suppose I want all the 1
in the fourth expansion, then the 2
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.4,1.4 | tr '' ' '
a1a1 a1b1 a2a1 a2b1 b1a1 b1b1 b2a1 b2b1 a1a2 a1b2 a2a2 a2b2 b1a2 b1b2 b2a2 b2b2
Suppose I want all the 1a
in the middle, then 1b
, then 2a
, then 2b
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.2,1.3 | tr '' ' '
a1a1 a1a2 b1a1 b1a2 a1b1 a1b2 b1b1 b1b2 a2a1 a2a2 b2a1 b2a2 a2b1 a2b2 b2b1 b2b2
You can even, just as easily, reverse any order in the expansions above, just adding and r
to the previous command; for example, the last one:
printf '%s' a..b1..2a..b1..2 | sort -rzk 1.2,1.3 | tr '' ' '
b2b2 b2b1 a2b2 a2b1 b2a2 b2a1 a2a2 a2a1 b1b2 b1b1 a1b2 a1b1 b1a2 b1a1 a1a2 a1a1
add a comment |
For this particular case, I think that the option given by Stéphane Chazelas is the best one.
On the other hand, when you expand more complex things, this option doesn't scale well. So, you can achieve the same with this:
printf '%s' a..c1..3 | sort -zk 1.2,1.2 | tr '' ' '
which returns:
a1 b1 c1 a2 b2 c2 a3 b3 c3
Seems a little messy, but now, I have a huge control in the order, just changing two chars in the command above; for example:
echo a..b1..2a..b1..2
this will expand to:
a1a1 a1a2 a1b1 a1b2 a2a1 a2a2 a2b1 a2b2 b1a1 b1a2 b1b1 b1b2 b2a1 b2a2 b2b1 b2b2
Suppose I want all the 1
in the second expansion, then the 2
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.2,1.2 | tr '' ' '
a1a1 a1a2 a1b1 a1b2 b1a1 b1a2 b1b1 b1b2 a2a1 a2a2 a2b1 a2b2 b2a1 b2a2 b2b1 b2b2
Suppose I want all the a
in the third expansion, then the b
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.3,1.3 | tr '' ' '
a1a1 a1a2 a2a1 a2a2 b1a1 b1a2 b2a1 b2a2 a1b1 a1b2 a2b1 a2b2 b1b1 b1b2 b2b1 b2b2
Suppose I want all the 1
in the fourth expansion, then the 2
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.4,1.4 | tr '' ' '
a1a1 a1b1 a2a1 a2b1 b1a1 b1b1 b2a1 b2b1 a1a2 a1b2 a2a2 a2b2 b1a2 b1b2 b2a2 b2b2
Suppose I want all the 1a
in the middle, then 1b
, then 2a
, then 2b
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.2,1.3 | tr '' ' '
a1a1 a1a2 b1a1 b1a2 a1b1 a1b2 b1b1 b1b2 a2a1 a2a2 b2a1 b2a2 a2b1 a2b2 b2b1 b2b2
You can even, just as easily, reverse any order in the expansions above, just adding and r
to the previous command; for example, the last one:
printf '%s' a..b1..2a..b1..2 | sort -rzk 1.2,1.3 | tr '' ' '
b2b2 b2b1 a2b2 a2b1 b2a2 b2a1 a2a2 a2a1 b1b2 b1b1 a1b2 a1b1 b1a2 b1a1 a1a2 a1a1
add a comment |
For this particular case, I think that the option given by Stéphane Chazelas is the best one.
On the other hand, when you expand more complex things, this option doesn't scale well. So, you can achieve the same with this:
printf '%s' a..c1..3 | sort -zk 1.2,1.2 | tr '' ' '
which returns:
a1 b1 c1 a2 b2 c2 a3 b3 c3
Seems a little messy, but now, I have a huge control in the order, just changing two chars in the command above; for example:
echo a..b1..2a..b1..2
this will expand to:
a1a1 a1a2 a1b1 a1b2 a2a1 a2a2 a2b1 a2b2 b1a1 b1a2 b1b1 b1b2 b2a1 b2a2 b2b1 b2b2
Suppose I want all the 1
in the second expansion, then the 2
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.2,1.2 | tr '' ' '
a1a1 a1a2 a1b1 a1b2 b1a1 b1a2 b1b1 b1b2 a2a1 a2a2 a2b1 a2b2 b2a1 b2a2 b2b1 b2b2
Suppose I want all the a
in the third expansion, then the b
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.3,1.3 | tr '' ' '
a1a1 a1a2 a2a1 a2a2 b1a1 b1a2 b2a1 b2a2 a1b1 a1b2 a2b1 a2b2 b1b1 b1b2 b2b1 b2b2
Suppose I want all the 1
in the fourth expansion, then the 2
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.4,1.4 | tr '' ' '
a1a1 a1b1 a2a1 a2b1 b1a1 b1b1 b2a1 b2b1 a1a2 a1b2 a2a2 a2b2 b1a2 b1b2 b2a2 b2b2
Suppose I want all the 1a
in the middle, then 1b
, then 2a
, then 2b
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.2,1.3 | tr '' ' '
a1a1 a1a2 b1a1 b1a2 a1b1 a1b2 b1b1 b1b2 a2a1 a2a2 b2a1 b2a2 a2b1 a2b2 b2b1 b2b2
You can even, just as easily, reverse any order in the expansions above, just adding and r
to the previous command; for example, the last one:
printf '%s' a..b1..2a..b1..2 | sort -rzk 1.2,1.3 | tr '' ' '
b2b2 b2b1 a2b2 a2b1 b2a2 b2a1 a2a2 a2a1 b1b2 b1b1 a1b2 a1b1 b1a2 b1a1 a1a2 a1a1
For this particular case, I think that the option given by Stéphane Chazelas is the best one.
On the other hand, when you expand more complex things, this option doesn't scale well. So, you can achieve the same with this:
printf '%s' a..c1..3 | sort -zk 1.2,1.2 | tr '' ' '
which returns:
a1 b1 c1 a2 b2 c2 a3 b3 c3
Seems a little messy, but now, I have a huge control in the order, just changing two chars in the command above; for example:
echo a..b1..2a..b1..2
this will expand to:
a1a1 a1a2 a1b1 a1b2 a2a1 a2a2 a2b1 a2b2 b1a1 b1a2 b1b1 b1b2 b2a1 b2a2 b2b1 b2b2
Suppose I want all the 1
in the second expansion, then the 2
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.2,1.2 | tr '' ' '
a1a1 a1a2 a1b1 a1b2 b1a1 b1a2 b1b1 b1b2 a2a1 a2a2 a2b1 a2b2 b2a1 b2a2 b2b1 b2b2
Suppose I want all the a
in the third expansion, then the b
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.3,1.3 | tr '' ' '
a1a1 a1a2 a2a1 a2a2 b1a1 b1a2 b2a1 b2a2 a1b1 a1b2 a2b1 a2b2 b1b1 b1b2 b2b1 b2b2
Suppose I want all the 1
in the fourth expansion, then the 2
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.4,1.4 | tr '' ' '
a1a1 a1b1 a2a1 a2b1 b1a1 b1b1 b2a1 b2b1 a1a2 a1b2 a2a2 a2b2 b1a2 b1b2 b2a2 b2b2
Suppose I want all the 1a
in the middle, then 1b
, then 2a
, then 2b
:
printf '%s' a..b1..2a..b1..2 | sort -zk 1.2,1.3 | tr '' ' '
a1a1 a1a2 b1a1 b1a2 a1b1 a1b2 b1b1 b1b2 a2a1 a2a2 b2a1 b2a2 a2b1 a2b2 b2b1 b2b2
You can even, just as easily, reverse any order in the expansions above, just adding and r
to the previous command; for example, the last one:
printf '%s' a..b1..2a..b1..2 | sort -rzk 1.2,1.3 | tr '' ' '
b2b2 b2b1 a2b2 a2b1 b2a2 b2a1 a2a2 a2a1 b1b2 b1b1 a1b2 a1b1 b1a2 b1a1 a1a2 a1a1
answered 5 hours ago
matsib.devmatsib.dev
47636
47636
add a comment |
add a comment |
Using a loop:
for l in a..b; do printf '%sn' "$l"1..3; done
This will loop through your first expansion and then expand each character with the second.
If you need the output all on one line you can remove the n
:
for l in a..b; do printf '%s ' "$l"1..3; done
This won't give you a trailing newline but if you are passing it to a command or variable that shouldn't be an issue.
add a comment |
Using a loop:
for l in a..b; do printf '%sn' "$l"1..3; done
This will loop through your first expansion and then expand each character with the second.
If you need the output all on one line you can remove the n
:
for l in a..b; do printf '%s ' "$l"1..3; done
This won't give you a trailing newline but if you are passing it to a command or variable that shouldn't be an issue.
add a comment |
Using a loop:
for l in a..b; do printf '%sn' "$l"1..3; done
This will loop through your first expansion and then expand each character with the second.
If you need the output all on one line you can remove the n
:
for l in a..b; do printf '%s ' "$l"1..3; done
This won't give you a trailing newline but if you are passing it to a command or variable that shouldn't be an issue.
Using a loop:
for l in a..b; do printf '%sn' "$l"1..3; done
This will loop through your first expansion and then expand each character with the second.
If you need the output all on one line you can remove the n
:
for l in a..b; do printf '%s ' "$l"1..3; done
This won't give you a trailing newline but if you are passing it to a command or variable that shouldn't be an issue.
edited 8 hours ago
answered 8 hours ago
Jesse_bJesse_b
16.9k34183
16.9k34183
add a comment |
add a comment |
bash, ksh, zsh
A one liner that works in (bash, ksh, zsh) (not all shells can do "Brace expansion" in reverse order):
$ echo 3..1c..a | rev
a1 b1 c1 a2 b2 c2 a3 b3 c3
An alternative that use eval
(which still is for bash,ksh,zsh and may be more criptic) is:
$ eval echo 'a..c'1..3
a1 b1 c1 a2 b2 c2 a3 b3 c3
To understand what happens, replace eval
with echo
:
$ echo echo 'a..c'1..3
echo a..c1 a..c2 a..c3
The command executed (after eval expansion) is actually echo a..c1 a..c2 a..c3
. Which expands as you want/need.
all shells
For all shells, use:
$ for i in 1 2 3; do for j in a b c; do printf "%s%s " "$j" "$i"; done; done; echo
a1 b1 c1 a2 b2 c2 a3 b3 c3
If you don't mind a trailing space.
If you must have no trailing space added:
$ s=""; for i in 1 2 3; do for j in a b c; do printf "%s%s" "$s" "$j" "$i"; s=" "; done; done; echo
a1 b1 c1 a2 b2 c2 a3 b3 c3
add a comment |
bash, ksh, zsh
A one liner that works in (bash, ksh, zsh) (not all shells can do "Brace expansion" in reverse order):
$ echo 3..1c..a | rev
a1 b1 c1 a2 b2 c2 a3 b3 c3
An alternative that use eval
(which still is for bash,ksh,zsh and may be more criptic) is:
$ eval echo 'a..c'1..3
a1 b1 c1 a2 b2 c2 a3 b3 c3
To understand what happens, replace eval
with echo
:
$ echo echo 'a..c'1..3
echo a..c1 a..c2 a..c3
The command executed (after eval expansion) is actually echo a..c1 a..c2 a..c3
. Which expands as you want/need.
all shells
For all shells, use:
$ for i in 1 2 3; do for j in a b c; do printf "%s%s " "$j" "$i"; done; done; echo
a1 b1 c1 a2 b2 c2 a3 b3 c3
If you don't mind a trailing space.
If you must have no trailing space added:
$ s=""; for i in 1 2 3; do for j in a b c; do printf "%s%s" "$s" "$j" "$i"; s=" "; done; done; echo
a1 b1 c1 a2 b2 c2 a3 b3 c3
add a comment |
bash, ksh, zsh
A one liner that works in (bash, ksh, zsh) (not all shells can do "Brace expansion" in reverse order):
$ echo 3..1c..a | rev
a1 b1 c1 a2 b2 c2 a3 b3 c3
An alternative that use eval
(which still is for bash,ksh,zsh and may be more criptic) is:
$ eval echo 'a..c'1..3
a1 b1 c1 a2 b2 c2 a3 b3 c3
To understand what happens, replace eval
with echo
:
$ echo echo 'a..c'1..3
echo a..c1 a..c2 a..c3
The command executed (after eval expansion) is actually echo a..c1 a..c2 a..c3
. Which expands as you want/need.
all shells
For all shells, use:
$ for i in 1 2 3; do for j in a b c; do printf "%s%s " "$j" "$i"; done; done; echo
a1 b1 c1 a2 b2 c2 a3 b3 c3
If you don't mind a trailing space.
If you must have no trailing space added:
$ s=""; for i in 1 2 3; do for j in a b c; do printf "%s%s" "$s" "$j" "$i"; s=" "; done; done; echo
a1 b1 c1 a2 b2 c2 a3 b3 c3
bash, ksh, zsh
A one liner that works in (bash, ksh, zsh) (not all shells can do "Brace expansion" in reverse order):
$ echo 3..1c..a | rev
a1 b1 c1 a2 b2 c2 a3 b3 c3
An alternative that use eval
(which still is for bash,ksh,zsh and may be more criptic) is:
$ eval echo 'a..c'1..3
a1 b1 c1 a2 b2 c2 a3 b3 c3
To understand what happens, replace eval
with echo
:
$ echo echo 'a..c'1..3
echo a..c1 a..c2 a..c3
The command executed (after eval expansion) is actually echo a..c1 a..c2 a..c3
. Which expands as you want/need.
all shells
For all shells, use:
$ for i in 1 2 3; do for j in a b c; do printf "%s%s " "$j" "$i"; done; done; echo
a1 b1 c1 a2 b2 c2 a3 b3 c3
If you don't mind a trailing space.
If you must have no trailing space added:
$ s=""; for i in 1 2 3; do for j in a b c; do printf "%s%s" "$s" "$j" "$i"; s=" "; done; done; echo
a1 b1 c1 a2 b2 c2 a3 b3 c3
answered 7 hours ago
IsaacIsaac
13.3k12159
13.3k12159
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f525921%2fapply-brace-expansion-in-reverse-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown