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 .)