bash逗号分隔值的相反顺序 [英] bash reverse order of comma separated values
本文介绍了bash逗号分隔值的相反顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下元素列表:
a, b, c, 1337, d, e
我希望我有
e, d, 1337, c, b, a
如何在bash中实现目标?
How can I achieve that in bash?
推荐答案
您可以使用 awk
:
You can do this with awk
:
#!/bin/bash
awk 'BEGIN{FS=",[ ]*"; OFS=", "}
{
for (i=NF; i>0; i--) {
printf "%s", $i;
if (i>1) {
printf "%s", OFS;
}
}
printf "\n"
}'
脚本的
说明:
-
FS =,[] *";
:字段分隔符的正则表达式(输入的分隔符)匹配一个逗号,后跟零个或多个空格,因此您的输入可以是以下任意一项:-
a,b,c,1337,d,e
-
a,b,c,1337,d,e
-
a,(很多空格)b,c,1337,d,e
FS=",[ ]*";
: The regex for Field Separator (delimiter for your input) matches a comma followed by zero or more spaces, so your input can be any of:a, b, c, 1337, d, e
a,b,c,1337,d,e
a, (many spaces) b, c,1337,d, e
示例用法:
$ ./script_name < input_file > output_file
这篇关于bash逗号分隔值的相反顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-
查看全文