get 2 strings next to known string

December 20, 2010 at 21:36:16
Specs: Linux
How could I search a particular string from a text file and remove everything else except next two strings in Linux script?

See More: get 2 strings next to known string

Report •

#1
December 21, 2010 at 07:02:01
How about providing an example of what you want?

Report •

#2
December 21, 2010 at 19:41:03
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).


Report •

#3
December 21, 2010 at 21:36:54
RRSAdmin

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:

#!/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"


Report •
Related Solutions


Ask Question