我如何将一些文本附加到没有临时文件的管道中

编程入门 行业动态 更新时间:2024-10-07 06:46:20
本文介绍了我如何将一些文本附加到没有临时文件的管道中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从一个程序有多个版本的目录中获取最大版本号

I am trying to get the max version number from a directory where i have several versions of one program

例如,如果ls的输出是

for example if output of ls is

something01_1.sh something02_0.1.2.sh something02_0.1.sh something02_1.1.sh something02_1.2.sh something02_2.0.sh something02_2.1.sh something02_2.3.sh something02_3.1.2.sh something.sh

我正在获得具有以下内容的最大版本号-

I am getting the max version number with the following -

ls somedir | grep some_prefix | cut -d '_' -f2 | sort -t '.' -k1 -r | head -n 1

现在,如果同时我想用系统中已有的版本号检查它,那么最好的方法是什么...

Now if at the same time i want to check it with the version number which i already have in the system, whats the best way to do it...

在bash中,我可以正常工作(如果当前版本为2.5)

in bash i got this working (if 2.5 is the current version)

(ls somedir | grep some_prefix | cut -d '_' -f2; echo 2.5) | sort -t '.' -k1 -r | head -n 1

还有其他正确的方法吗?

is there any other correct way to do it?

在上面的示例中,some_prefix是something02.

In the above example some_prefix is something02.

这里的实际问题是

(ls smthing; echo more) | sort

这是将两个命令/程序的输出合并到第三个命令的最佳方法.

is it the best way to merge output of two commands/program for piping into third.

推荐答案

我找到了解决方案.看来最好的方法是使用流程替换.

I have found the solution. The best way it seems is using process substitution.

cat <(ls smthing) <(echo more) | sort

以我的版本示例

cat <(ls somedir | grep some_prefix | cut -d '_' -f2) <(echo 2.5) | sort -t '.' -k1 -r | head -n 1

为了将来的读者的利益,我建议-请放弃一线诱饵,并使用glob作为 chepner 建议.

for the benefit of future readers, I recommend - please drop the lure of one-liner and use glob as chepner suggested.

在超级用户上提出了几乎类似的问题.

Almost similar question is asked on superuser.

有关流程替换的更多信息.

更多推荐

我如何将一些文本附加到没有临时文件的管道中

本文发布于:2023-11-02 06:20:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   临时文件   道中   文本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!