Link to home
Start Free TrialLog in
Avatar of doug5516
doug5516

asked on

Find a pattern in a variable

Can anyone tell me how to find a pattern in a string of text in a variable using Bash in Linux?

Example:
var=Line 1: This is a line

I want to be able to search var to determine if : is part of the text.  If var does contain ":", I would like to be able to split it so that I can assign Line 1 to var1 and This is a line to var2.
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

try

line=`grep "var=" file`
echo $line | grep ":"
if [ $? -eq 0 ]
then
      var2=`echo $line | awk -F":" '{ print $2 }'`
      var=`echo $line | awk -F"=" '{ print $2 }'`
fi
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial