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 •

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

Report •

December 21, 2010 at 19:41:03
i have a file named file.txt with contents given below,

/href="artifact/">RRSAdmin- </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 ( 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 •

December 21, 2010 at 21:36:54

OK, based on your spec, the version is:
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:


totvar=$(awk ' BEGIN { FS="/" }
for(i=1; i<=NF; i++)
   if(match($i, "^[0-9]") )
      # field before version
      # 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