如何使用sed/awk在两个模式之间提取文本 [英] How to extract text between two patterns with sed/awk

查看:90
本文介绍了如何使用sed/awk在两个模式之间提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里已经被问过1000次了,但是我读了很多类似的问题,但仍然没有找到正确的方法来做.我需要从看起来像这样的一行中提取一个数字:

I know this has been asked 1000 times here, but I read a lot of similar questions and still did not manage to find the right way to do this. I need to extract a number from a line that looks like this:

{"version":"4.9.123M","info":{"version":[2034.2],"description":""},"status":"OK"}

预期输出:

2034.2

此版本号不一定总是相同,但其余行应该相同.

This version number will not always be the same, but the rest of the line should.

我曾尝试使用sed,但我对此并不陌生,但失败了:

I have tried working with sed but I am new to this and failed:

 sed -e 's/version":[\(.*\),"description/\1/'

输出:

sed: -e expression #1, char 35: unterminated `s' command

我认为问题在于,该行中包含太多特殊字符,我编写的命令不太好.

I think the issue is that there are too many special characters involved in the line and I did not write the command very well.

推荐答案

如果版本始终包含在[]中,并且一行中没有其他[或],则可以尝试以下逻辑

If the version is always enclosed in [] and no other [ or ] is present in a line ,you can try this logic

STR='{"version":"4.9.123M","info":{"version":[2034.2],"description":""},"status":"OK"}'
echo $STR | awk -F'[' '{print $2}' | awk -F']' '{print $1}'

这篇关于如何使用sed/awk在两个模式之间提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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