[英]how can i cut off the strings from an output in Bash shell?
我運行的命令如下:
rpm -qi setup | grep Install
命令的輸出:
Install Date: Do 30 Jul 2020 15:55:28 CEST
我想進一步編輯此輸出,以便僅保留: 30 Jul 2020
其余的輸出不顯示。
在 bash 中什么最好的編輯方式可以簡單地得到這個最終結果?
像這樣使用grep -Po
( -P
= 使用 Perl 正則表達式引擎, -o
= 只打印匹配項,而不是整行):
echo '**Install Date: Do 30 Jul 2020 15:55:28 CEST**' | grep -Po '\d{1,2}\s+\w{3}\s+\d{4}'
您也可以像這樣使用cut
( -d' '
= 在空白處拆分, -f4-6
= 打印字段 4 到 6):
echo '**Install Date: Do 30 Jul 2020 15:55:28 CEST**' | cut -d' ' -f4-6
輸出:
30 Jul 2020
您可以僅使用rpm
queryformat 和bash
printf 來完成:
$ printf '%(%d %b %Y)T\n' $(rpm -q --queryformat '%{INSTALLTIME}\n' setup)
29 Apr 2020
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.