我如何将一些文本附加到没有临时文件的管道中 [英] How do i append some text to pipe without temporary file

查看:82
本文介绍了我如何将一些文本附加到没有临时文件的管道中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

这篇关于我如何将一些文本附加到没有临时文件的管道中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆