对于后&QUOT提取一切常规的前pression;:"在bash命令行输出 [英] regular expression for extracting everything after ":" of a command line output in bash

查看:168
本文介绍了对于后&QUOT提取一切常规的前pression;:"在bash命令行输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有当前存储的命令行输出到一个变量bash脚本。我想执行常规的前pression抽取一切最后的冒号之后()。

I have a bash script that currently stores a command line output into a variable. I want to perform regular expression to extract everything after the last colon (:).

这是我的code:

line="$(command line)"
echo "$line" | awk '/[^:]+$/'

这不幸的是不起作用。我应该如何解决?

This unfortunately doesn't work. How should I fix it?

推荐答案

使用GNU bash和它的参数扩展

With GNU bash and its Parameter Expansion:

line="root:x:0:0:root:/root:/bin/bash"
echo "${line/*:/}"

输出:


/bin/bash


使用GNU bash和正则表达式:


With GNU bash and regex:

line="root:x:0:0:root:/root:/bin/bash"
[[ $line =~ ([^:]*)$ ]] && echo "${BASH_REMATCH[1]}"

输出:


/bin/bash

这篇关于对于后&QUOT提取一切常规的前pression;:"在bash命令行输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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