get characters from output of a command in a variable

Hi,

i have two questions, I am new to programming

  1. I have an output of a command and i want to get some specific part of it in a variable. i am trying

sr=`some comand xyz| grep 'Last Changed Rev:' | cut -c19-`
now variable sr gets a end of line character at end.

output of the command is

Date: yyyy-mm-dd
---------

abc
xyz
Last Changed Rev: 10
abc

i want to get this 10 in the variable sr. i dont know the number length in advance.

  1. Also the comand returns multiple lines output, how to get complete text in between

abc
xyz
Last Changed Rev: 10
abc

in another variable?
please help!

Thanks in advance..

To get only the "10" from that kind of line into a variable (and it doesn't matter how long this number will become because we only fetch the last field):

With awk:

VAR=`awk '/^Last Changed Rev:/ {print $NF}' nameofoutputfile`

With sed:

VAR=`sed 's/^Last Changed Rev: *\([^ ]*\)$/\1/g' nameofoutputfile`

To cut off the header let's use awk again. Let's say the data you want starts at line number 3:

cat nameofoutputfile | awk 'NR > 2 {print}'

zaxxon preceded me :slight_smile: However:

text=`some comand xyz | sed '/abc/,/abc/!d'`

sr=`echo "$text" | awk '/Last Changed Rev:/ { print $NF }'`

thanks , its is done :slight_smile: