用grep解析字符串 [英] Parsing string with grep

查看:120
本文介绍了用grep解析字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个字符串:

  [INFO]总时间:2分钟8秒

只获得

  2分钟8秒


解决方案

如果行前缀始终相同,只需使用sed并用空字符串替换前缀即可:

  sed's / \ [INFO \]总时间://'

假设时间总是冒号后最后一行,请使用以下正则表达式(用冒号后面的所有内容替换每行):

  sed's /^.*:\(。* \)$ / \ 1 /'


I need some help with parsing a string in Linux.

I have a string:

[INFO] Total time: 2 minutes 8 seconds

and want to get only

2 minutes 8 seconds

解决方案

If the line prefix is always the same, simply use sed and replace the prefix with an empty string:

sed 's/\[INFO\] Total Time: //'

Assuming that the time is always the last thing in a line after a colon, use the following regex (replace each line with everything after the colon):

sed 's/^.*: \(.*\)$/\1/'

这篇关于用grep解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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