在 KSH 中使用正则表达式设置另一个变量
我有一个 Korn shell 脚本,我想根据另一个变量和正则表达式更改一个变量。
我想要发生的是生成如下所示的变量值,但不调用 sed:
$ echo 'orl,bdl,lap' | sed "s/,*orl//" | sed "s/^,*//"
bdl,lap
$ echo 'orl,bdl,lap' | sed "s/,*bdl//" | sed "s/^,*//"
orl,lap
$ echo 'orl,bdl,lap' | sed "s/,*lap//" | sed "s/^,*//"
orl,bdl
我尝试过各种变体
export b="orl,bdl,lap"
export a=${b}*(,*lap)
,但通常会出错。这可能吗?
我已经看到了这一点:
if [[ $var = fo@(?4*67).c ]];then ...
所以它应该像处理文件名一样工作。
I have a Korn shell script that I would like to change a variable based on another and a regex.
What I want to happen is to generate a variable value like below, but without calling sed:
$ echo 'orl,bdl,lap' | sed "s/,*orl//" | sed "s/^,*//"
bdl,lap
$ echo 'orl,bdl,lap' | sed "s/,*bdl//" | sed "s/^,*//"
orl,lap
$ echo 'orl,bdl,lap' | sed "s/,*lap//" | sed "s/^,*//"
orl,bdl
I've tried variations of
export b="orl,bdl,lap"
export a=${b}*(,*lap)
but usually get an error. Is this possible?
I've seen this:
if [[ $var = fo@(?4*67).c ]];then ...
so it should work like it does with filenames.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是这样的吗?
echo 'orl,bdl,lap' | echo 'orl,bdl,lap' | cut -d"," -f3,2
将
-f3,2
更改为您可能需要的其他字段。另外,如果您需要更好的正则表达式构造,您可以使用 awk,但是我需要您提供更好的详细信息以了解您需要什么转换。
Is it something like this?
echo 'orl,bdl,lap' | cut -d"," -f3,2
Change the
-f3,2
to other fields that you may need.Also if you need better regex construction you could use
awk
, but then I need you provide better details to understand what transformation you need.一种方法是使用
IFS
:或
或者使用正则表达式匹配:
One way to do this is to use
IFS
:or
Or using regex matching:
这是所问问题的实际答案:
请参阅 ksh93 手册页< /a> 了解详细信息:您必须将有关模式和模式列表的材料放在 文件名生成,其中包含 参数扩展 来完全解决这个问题。
请注意,虽然您不能在变量扩展中直接连续应用多个正则表达式(就像使用
sed
那样,例如sed "s/,*orl//;s/^, *//"
),您可以将两个变量赋值放在同一条语句中,它们将从左到右连续执行。here's the actual answer to the question as asked:
see the ksh93 manpage for details: you have to put together the material on patterns and pattern-lists under file name generation with the material on search-and-replace variable expansions under parameter expansion to figure this out completely.
note that while you can't directly apply multiple regexes in succession in a variable expansion (as you could have with
sed
, e.g.sed "s/,*orl//;s/^,*//"
), you can put two variable assignments in the same statement, and they'll execute in succession, left to right.