Get Two Strings Next To Known String

I have a file named file.txt with contents given below:

/href="artifact/com.apple.rrs.app/RRSAdmin/3.2.0.1-SNAPSHOT/RRSAdmin-3.2.0.1-SNAPSHOT.jar">RRSAdmin-3.2.0.1-SNAPSHOT.jar </li></ul></td></tr><tr><td><a></td><td style="vertical-align:middle"><a>No changes.</td></tr></table></td></tr></table><table width="100%"><tr><td id="footer"><span style="padding-right:2em; color:gray">

In this I want to extract only the version number 3.2.0.1 ( which change with the file). I don't want to use command: cut. Also as you can see, the string on both sides of version number is known ( but vary with file) which is available as a variable ($VAR1 and $VAR2).

Solution:

OK, based on your spec, the version is: 3.2.0.1.

var1 is RRSAdmin (the field before the version field if the field seperator is /)
var2 is: -SNAPSHOT (the string after the version)

This awk script should do it:

Code:

#!/bin/ksh

totvar=$(awk ' BEGIN { FS="/" }
{
for(i=1; i<=NF; i++)
   {
   if(match($i, "^[0-9]") )
      {
      # field before version
      var1=$(i-1)
      var2=$i
      # version removing unwanted chars
      gsub("[A-Za-z-]","" ,$i)
      # remove version giving field after version
      gsub("[0-9.]","" ,var2)
      printf("%s %s %s", var1, $i, var2)
      }
   }
} ' file.txt)

# parse the variable string from awk
set - $(echo $totvar) 
echo "var1 is: $1"
echo "version is: $2"
echo "var2 is: $3"

Have a Linux Problem
Linux Forum - Do you have a Linux Question?

Linux Books
Linux Certification, System Administration, Programming, Networking Books

Linux Home: Linux System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.