Thursday, July 23, 2015

Grep between two words in string

Say from the string "Age: = 24 years", you want to extract the value 24. Grep can be used to search for a pattern between two words. Here is the grep command for that:
echo "Age: = 24 years" | grep -P -o '(?<=Age: = ).*(?= years)'

Monday, July 6, 2015

Range or Sequence in Shell Script

Following is an example to print numbers 1 to 20 in a shell script. The numbers below 10 need to be printed with 2 digits like 01..09 

#!/bin/bash
for i in $(seq 20) 
do 
if [ $i -le 9 ] 
then echo 0$i
else echo $i
fi
done