3

Just curious if there's a way I can show the output between two markers?

I was trying grep -r "<cron_expr>.*</run>" * but it doesn't seem to be returning as expected. Which is to say its not showing newline breaks?

So I guess the the real question is how can i find out what between two markers regardless of newline BS. I can't install on this system so no pcregrep =(

Maybe AWK?

(P.s. someone should make an AWK tag)

<crontab>
    <jobs>
        <enterprise_reward_balance_warning_notification>
            <schedule><cron_expr>1 * * * *</cron_expr></schedule>
            <run><model>enterprise_reward/observer::scheduledBalanceExpireNotification</model></run>
        </enterprise_reward_balance_warning_notification>
        <enterprise_reward_expire_points>
            <schedule><cron_expr>1 * * * *</cron_expr></schedule>
            <run><model>enterprise_reward/observer::scheduledPointsExpiration</model></run>
        </enterprise_reward_expire_points>
    </jobs>
</crontab>

I just want to show whats on the <schedule> and <run> line(s if more than one, like above) anytime I see and what file they're in.

2
  • grep and sed are both line-oriented. awk can use an arbitrary delimiter in place of newline, but still needs some kind of delimiter. What exactly are you looking for? You may be going about it the wrong way (for example, none of these tools is really appropriate for HTML/XML, and not ideal for JSON).
    – geekosaur
    May 4, 2012 at 21:45
  • Yeah i'm searching through a bunch of config.xml's on our server to try and figure out when and what they're running in their private crontab, i'll post an example in a minute
    – ehime
    May 4, 2012 at 22:07

2 Answers 2

2

AWK script

AWK is probably the answer using the /start/,/stop/ pattern like this:

/<schedule>/,/<\/schedule>/ {
    print;
}
/<run>/,/<\/run>/ {
    print;
}

This will print everything in the schedule and run tags (including the tags) and will preserve new lines. Adapt it to print what you need. To run it:

cat myfile.txt | awk -f script.awk

XSLT style sheet

Here's an XSLT version that should do something very similar. Run it using xsltproc, which should be installed by default.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="schedule">
    <xsl:value-of select="." />
</xsl:template>

<xsl:template match="run">
    <xsl:value-of select="." />
</xsl:template>

</xsl:stylesheet>
3
  • I can't install new things on our webserver so I need to work with the tools I have (grep sed/awk) =( Could I spit out all content between two instances of crontab?
    – ehime
    May 4, 2012 at 22:21
  • How do you do the awk one without including the search tags?
    – rynop
    Sep 10, 2012 at 13:09
  • AWK may not be the right tool if you want to exclude the search tags because it is line oriented so basically it matches a start line and an end line. If you want to exclude the delimiters, the XSLT version will be more adapted because XSLT understands the XML structure and will let you manipulate child nodes. In practice the XSLT example above strips out all XML tags and outputs the text only. Sep 10, 2012 at 16:36
0

There are lots of ways to do what I think you want too do.

you could use sed

say I wanted to get the numbers in between lspci -nn

it spits out numbers like this

[1234:4321]

I would do this

lspci -nn| sed 's/.*\[\(.*\)\].*/\1/'

This is my first post here so I hope the escape character's are correct. But I am not sure what you are trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.