title: Bash RegEx Lookahead/Lookbehind CPU Information
body: <p>I'm simply storing the output of cat <code>/proc/cpuinfo</code> into a variable within a bash script. </p>
  
  <p><strong>/proc/cpuinfo:</strong></p>
  
  <pre><code>processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 58 model name : Intel(R) Core(TM) i5-3230M CPU @ 2.80GHz stepping : 9 cpu MHz : 2562.759 cache size : 6144 KB fpu : yes fpu_exception : yes cpuid level : 5 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc up rep_good nopl pni monitor ssse3 lahf_lm bogomips : 5125.51 clflush size : 64 cache_alignment : 64 address sizes : 36 bits physical, 48 bits virtual power management:
  </code></pre>
  
  <p>When I cat the info to a text file and grep it: </p>
  
  <pre><code>grep -P '(?&lt;=model name\s:\s)[\w ()-]*(?=CPU)' infotext.txt
  </code></pre>
  
  <p>I get a match: </p>
  
  <pre><code>model name  : **Intel(R) Core(TM) i5-3230M** CPU @ 2.60GHz
  </code></pre>
  
  <p>However upon testing this within my bash script I get no match:</p>
  
  <pre><code>regex='(?&lt;=model name\s:\s).*(?=CPU)'
  
  if [[ $line =~ $regex ]]; then
       modelName=${BASH_REMATCH[1]}
  fi
  </code></pre>
  
  <p>Might this have something to do with the differences between cat-ing the output of /proc/cpuinfo into a text file versus simply storing it in a variable <code>cpuInfo=$(cat /proc/cpuinfo)</code> ?</p>
  
  <p>If not, is the formatting of my regex within bash causing a problem? (I may need an alternate way of doing lookahead/lookbehind for 'model name : ' and 'CPU').</p>