Bash - 在单行中发送多个命令 [英] Bash - sed multiple commands in single line

查看:66
本文介绍了Bash - 在单行中发送多个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

玩 sed - 下面的命令可以满足要求,但一个衬垫会更好.我尝试将前两个命令(带有分隔的 ';')组合起来以删除尾随的 ':' ,但没有成功.否则,就求助于删除最后一个':'并写入一个新文件以执行下一个操作.

Playing with sed - the commands below do what's required but one liners would be better. I have tried combining the first two commands (with a separating ';') to remove the trailing ':' without success. Otherwise, resorted to removing the last ':' and writing to a new file to perform the next operation.

文件sys",单行包含可变数字字符和:"分隔符.例如;

File 'sys' with a single line containing a variable number characters and ':' separator. For example;

输入 - 'sys' 第一行 3.000000:50: 想要的输出,两个变量 thd=3 mem=50

Input - 'sys' first line 3.000000:50: desired output, two variables thd=3 mem=50

thd=$(echo | sed 's/.......:.*//' < sys)
sed 's/:$//' < sys > sys1
mem=$(echo | sed 's|........:||' < sys1)

有没有办法组合前两个 sed 命令以避免写入第二个文件?我已经尝试了各种方法

Is there a way to combine the first two sed commands to avoid writing a second file? I have tried this various ways

类似这样的东西 - 这是删除尾随 ':' 的错误顺序

Something like this - this is the wrong order to remove the trailing ':'

thd=$(echo | sed 's/:$//;s/.......:.*//' < sys)
mem=$(echo | sed 's|........:||' < sys1)

输出 3 50: 附加分隔符.

这是正确的顺序并产生所需的输出.Bash 不会将第一次操作的结果保存在文件 sys.path 中.我应该在 3 号班轮中捡到的.

This is the correct order and produces the desired output. Bash does not save the result of the first operation in the file sys. Which I should have picked up in the 3 liner.

thd=$(echo | sed 's/.......:.*//' < sys)
mem=$(echo | sed 's|........:||;s/:$//' < sys)

推荐答案

如果你需要两个变量独立赋值,第一个包含点前的数字,第二个包含冒号之间的数字,你可以使用一种方法喜欢

If you need two variables to be assigned values independently, the first containing the number before the point and the second the number between the colons, you can use an approach like

thd=$(cut -f1 -d. < sys)
mem=$(cut -f2 -d: < sys)

也可以同时分配:

read thd mem < <(tr "." ":" < sys | cut -f1,3 -d: --output-delimiter=" ")

这篇关于Bash - 在单行中发送多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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