如何使用bash脚本从标记之间的文件文本中提取 [英] How to extract from a file text between tokens using bash scripts

查看:201
本文介绍了如何使用bash脚本从标记之间的文件文本中提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了这个问题:<一href=\"http://stackoverflow.com/questions/4857424/extract-text-from-between-2-tokens-in-a-text-file-using-bash\">Extract从使用bash 标记在一个文本文件2之间的文本
因为我有一个非常类似的问题...
我要提取(并将其保存在打印之前$变量)在此XML文件文本:

I was reading this question: Extract text from between 2 tokens in a text file using bash because I have a very similar problem... I have to extract (and save it to $variable before printing) text in this xml file:

<--more labels up this line>
<ExtraDataItem name="GUI/LastVMSelected" value="14cd3204-4774-46b8-be89-cc834efcba89"/>
<--more labels and text down this line-->

我只需要获得价值=(显然不带括号,没有价值='),但首先,我认为它必须要搜索GUI / LastVMSelected得到这条线,因为有可能是一个类似的值场其他线路,而标签的值就是我想要的。

I only need to get the value= (obviously without brackets and no 'value='), but first, I think it have to search "GUI/LastVMSelected" to get to this line, because there could be a similar value field in other lines,and the value of that label is that i want.

推荐答案

如果他们是在同一行(因为他们似乎是从你的例子),那就更简单了。刚:

If they are on the same line (as they seem to be from your example), it's even easier. Just:

sed -ne '/name="GUI\/LastVMSelected"/s/.*value="\([^"]*\)".*/\1/p'

说明:


  • -n:燮preSS默认的打印

  • / NAME =GUI \\ / LastVMSelected/:仅此模式匹配的行

  • S /。的值=([^])。的/ \\ 1 / P

    • 代替一切,捕获括号部分(value的值)

    • 和打印结果

    • -n: Suppress default print
    • /name="GUI\/LastVMSelected"/: only lines matching this pattern
    • s/.value="([^"])"./\1/p
      • substitute everything, capturing the parenthesized part (the value of value)
      • and print the result

      这篇关于如何使用bash脚本从标记之间的文件文本中提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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