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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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