简体   繁体   English

如何在鱼壳中生成字母序列表

[英]How to generate letter sequence list in fish shell

In bash you can generate letter sequence easily as "{a..z}", for example例如,在 bash 中,您可以轻松地将字母序列生成为“{a..z}”

$ echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z

How to do that in the fish shell instead?如何在鱼壳中做到这一点?

Fish doesn't support ranges in brace expansion , only comma-separated values: {a,b,c} . Fish 不支持大括号扩展中的范围,仅支持逗号分隔值: {a,b,c}

Thus, we are forced to search for a command capable of generating such sequence.因此,我们被迫搜索能够生成此类序列的命令。 For example, you can use Perl:例如,您可以使用 Perl:

for c in (perl -e '$,="\n"; print ("a" .. "z")')
  printf ">> %s\n" "$c"
end

where $, is the output field separator.其中$,是输出字段分隔符。

Output输出

>> a
>> b
...(skipped)
>> y
>> z

You may find this table useful.您可能会发现表很有用。

One way is to use printf and seq.一种方法是使用 printf 和 seq。

echo -e (printf '\\\x%x\n' (seq 97 122))

This works by generating "\\x61 \\x62 \\x63 ... \\x7a" which is then interpreted by "echo -e" as hex character codes.这通过生成“\\x61 \\x62 \\x63 ... \\x7a”来工作,然后由“echo -e”解释为十六进制字符代码。

Specifically to generate az sequence you can loop with, I think the most elegant and concise solution is to directly use bash with fmt (which is available in most unix-like environment).特别是为了生成可以循环使用的 az 序列,我认为最优雅和简洁的解决方案是直接使用 bash 和fmt (在大多数类 unix 环境中都可用)。 You don't even need Perl and its more readable:你甚至不需要 Perl 并且它的可读性更强:

for c in (bash -c "echo {a..z}" | fmt -w1)
    echo "hello $c"
end

The logic goes like this:逻辑是这样的:

  1. The for-loop in fish shell loops through both array values and lines. fish shell 中的 for 循环循环遍历数组值和行。 If you can generate az each in its separate line, you can loop with that in fish.如果您可以在单独的行中生成 az 每个,则可以在 fish 中循环。

  2. The command bash -c "echo {a..z}" would give you a single long string in fish:命令bash -c "echo {a..z}"会给你一个鱼的长字符串:

     abcdefghijklmnopqrstu vwxyz
  3. The command fmt -w1 would cut the space-separated strings into lines.命令fmt -w1会将空格分隔的字符串切割成行。

Result:结果:

hello a
hello b
hello c
... (skipped)
hello z

This is amazing because you can take advantage of the same principle to reuse bash syntax in fish.这很棒,因为您可以利用相同的原理在 fish 中重用 bash 语法。 Seems really flexible to me :-)对我来说似乎非常灵活:-)

Linux has the seq command for this, and it works with fishshell: Linux 有用于此的seq命令,它适用于 fishshell:

$ for f in (seq 1 10)
    echo $f
end
1
2
3
4
5
6
7
8
9
10
$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM