[英]How do I concatenate the result of two linux commands and have the result on one line?
[英]How to concatenate the result of two linux commands?
有沒有一種簡單的方法可以在一行中將兩個linux命令的結果連接起來? (即不使用變量)
我從附近的氣象站獲取當地的室外溫度。 今天的結果是:
5.2
我只想添加單位,因此結果應如下所示:
5.2°C
一個示例命令,幾乎可以得到我想要的:
wget -q -O- "http://meteocentre.com/montreal/home_e.html" | grep -oP '(?<=Tn= ).*(?=°)' ; printf "°C\\n"
您已經在串聯結果。
wget .. | grep ..
wget .. | grep ..
輸出: 5.2\\n
printf
輸出°C\\n
因此,級聯結果為5.2\\n°C\\n
,正是您得到的結果。
您要做的是在5.2
之后刪除換行符。 您可以利用$(command substitution)
去除尾隨換行符並將其傳遞給printf
的事實:
printf '%s°C\n' "$(wget -q -O- "http://meteocentre.com/montreal/home_e.html" | grep -oP '(?<=Tn= ).*(?=°)')"
在bash中,在末尾添加管道:
echo $(cat) [more text]
要么
echo `cat` [more text]
例:
wget -q -O- "http://meteocentre.com/montreal/home_e.html" | grep -oP '(?<=Tn= ).*(?=°)' | echo $(cat) °C
-> 5.2°C
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.