English 中文(简体)
两条插图之间的先导案文 [Shell]
原标题:Getting text between first occurance of two strings [Shell]

我有一口 feed子,看着这样的东西。 我想要做的是贪.。 html来自这一饲料。 (基本上,最大的项目是ur) 任何关于如何这样做的想法?

<rss>
<item>
    <title>ABC</title>
    <url>
        test.html
    </url>
</item>
<item>
    <title>CDE</title>
    <url>
        test1.html
    </url>
</item>
</rss>

感谢!

最佳回答

如果结构是定的,而且你知道《欧洲统一法》有定点,那么你可以做的是:

  cat <yourfile> | grep ".html" | head -n1

If you don t know the postfix (or the string "html" can exist before), you can do:

 cat <yourfile> | grep -A1 "<url>" | head -n2 | tail -n1

EDIT In case, the structure is not fixed (i.e., no newlines), there this

 cat <yourfile> | grep -o "<url>[^<]*</url>" | head -n1 | cut -d >  -f2 | cut -d <  -f1

 cat <yourfile> | grep -o "<url>[^<]*</url>" | head -n1 | sed -E -e"s#<url>(.*)</url>#1#"

可以工作。

问题回答

这或许有助于你:

 sed  /<url>/,/</url>/{//d;s/ *//;q};d  file.xml

这份文稿应当发挥作用:

awk  /<url>/ && url==0 {url=1;next;} {if(url==1) {print;url=2;}}  file

<>光线>

Following grep command might also work:

grep -m 1 "^ *<url>" -A1 file | grep -v "<url>"

我建议,不要使用基于线的工具,而是利用Xsl的转变,将你想要的数据从文件中删除,而不要对其格式作假设。

如果您将这一条保留到get-url.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xsl:output method="text"/>
        <xsl:template match="/">
                    <xsl:value-of select="normalize-space(rss/item/url)"/>
        </xsl:template>
</xsl:stylesheet>

Then you can get the value of url from feed.xml like this:

$ xsltproc get-url.xsl feed.xml; echo
test.html
$

额外<代码>echo只是给你在产出结束之后新行,使之便于使用交互式剪辑。 如果你将结果重新分配给一个票价变量($()。





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签