I want to get the variables $color and $number from a string that in general is like this: something, numColor (number). The color might be W, U, B, R, G. If there is no color the variable color should be C if the string before the comma doesn't have the word land or L otherwise. If there is more than one color the variable $color should be M. Here are some examples of what the string may look like and what the variables should be:Sorcery, R (1)                                $color=R,  $number=1Creature  Beast 5/3, 4G (5)                  $color=G   $number=5Sorcery, 1WWU (4)                             $color=M   $number=4Legendary Land                                $color=L   $number=0Artifact, 0                                   $color=C   $number=0Legendary Creature  Eldrazi 15/15, 15 (15)   $color=C   $number=15
Copying two words from a string into two variables
scripting;string
You can do task by sed on-liner:sed '/(\?\([0-9]\+\))\?$/s//; number=\1/     t n     s/$/; number=0/     :n     /^.*, [0-9]\?\([WURBG]\)/{                               s//color=\1/                               s/[WURBG]\{2,\}/M/                              }     /[Ll]and/s/^[^;]*/color=L /     /color/!s/^[^;]*/color=C /' fileBut I'd like to offer put commands in script file:#!/bin/sed -f/(\?\([0-9]\+\))\?$/s//; number=\1/t ns/$/; number=0/:n/^.*, [0-9]\?\([WURBG]\)/{    s//color=\1/    s/[WURBG]\{2,\}/M/}/[Ll]and/s/^[^;]*/color=L //color/!s/^[^;]*/color=C /then dosed -f script.file filePlease note that all above regex are tested on your example only and if you meet unproper works its can be settled by a little tune up. 