Extract code between 2 strings.

Hi,

Im having some problems with this. I have loaded a file with html code. All code is placed in the same line. I want to get everything between two given strings (including these strings and get only the first appearance).

Example:

File contains

<html><body><a href='a.html'>abc</a><a href='b.php'>dfd</a><</body></html>

Id like to get from string "<a href='a.html'>" to string "</a>"

The output would be:

<a href='a.html'>abc</a>

(as I said incluiding both strings and only first appareance)

I have tried several ways with sed but didnt succeed.

Thanks in advance

Try:

perl -nle '/<a.*?<\/a>/;print $&' file.html

Please use code tags as required by forum rules!

Show your several ways with sed .

Thanks. I will try with Perl. Ill let you know about it. Any option using sed?

To clear it up, string limits may be any string. Using the first example:

<html><body><a href='a.html'>abc</a><a href='b.php'>dfd</a><</body></html>

from "<a"
to ".php'"

Expected output:

<a href='a.html'>abc</a><a href='b.php'

Thanks

I think a relatively complicated sed script could do this, but I'm getting lazy in my old age. It is fairly straight forward to do it with an awk script (as long as you aren't worried about case insensitive matching). Some implementations of awk provide a case insensitive search option, but it is not included in the standards (and isn't available on the OS X laptop I use when testing solutions for this forum). If that is a problem, the following script could be easily extended to convert the input tag variable to a case-insensitive regular expression in a BEGIN section action.

Is use the Korn shell, but the following will work with any POSIX conforming shell (such as bash and ksh). If you are using a Solaris/SunOS system, change the awk in this script to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .

#!/bin/ksh
if [ $# -ne 2 ]
then    printf "Usage: %s tag file\n" "${0##*/}" >&2
        exit 1
fi
awk -v tag="$1" '
{       if(match($0, "</" tag ">")) $0 = substr($0, 1, RSTART + RLENGTH - 1)
        else printf("</%s> not found\n", tag)
        if(match($0, "<" tag "(>| ([^>])*>)")) $0 = substr($0, RSTART)
        else printf("<%s> not found\n", tag)
        print
}' "$2"

Save this script in a file (such as gettag), make it executable using:

chmod +x gettag

and invoke it with two operands (the 1st being the tag you want to extract, and the 2nd being the name of the file containing your html code line). Assuming that the html code in the 1st message in this thread is stored in a file named "File", the command:

./gettag a File

produces the output:

<a href='a.html'>abc</a>

The command:

./gettag body File

produces the output:

<body><a href='a.html'>abc</a><a href='b.php'>dfd</a><</body>

And, the command:

./gettag bod File

produces the output:

</bod> not found
<bod> not found
<html><body><a href='a.html'>abc</a><a href='b.php'>dfd</a><</body></html>

Note that awk and sed behavior is only defined by the standards when the input files they are processing are text files. So, if your single line html file is more than LINE_MAX bytes long, the results are unspecified. On standards conforming implementations, LINE_MAX can be as small as 2048. (You can find the value used on your system by running the command getconf LINE_MAX .)

Try

$ sed -r 's#^.*(<a href=.a\.html.>[^<]*</a>).*$#\1#' file
<a href='a.html'>abc</a>