{"id":146,"date":"2008-07-11T17:50:47","date_gmt":"2008-07-12T01:50:47","guid":{"rendered":"http:\/\/www.curlybrace.com\/words\/?p=146"},"modified":"2008-10-15T22:31:27","modified_gmt":"2008-10-16T06:31:27","slug":"extracting-a-version-number","status":"publish","type":"post","link":"https:\/\/www.curlybrace.com\/words\/2008\/07\/extracting-a-version-number\/","title":{"rendered":"Extracting a Version Number From a Text File Using Batch Files and Cygwin"},"content":{"rendered":"<p>We have a version.h file.  It contains lines such as:<\/p>\n<blockquote>\n<pre>#define VERSION_MAJOR     3\r\n#define VERSION_MINOR     1<\/pre>\n<\/blockquote>\n<p>By piping this through <tt>grep<\/tt>, I can get the line I&#8217;m looking for:<\/p>\n<blockquote>\n<pre>c:\\>grep \"VERSION_MAJOR\" version.h\r\n#define VERSION_MAJOR     3<\/pre>\n<\/blockquote>\n<p>But what I really want is the version number within the line, so I pipe this through <tt>sed<\/tt>:<\/p>\n<blockquote>\n<pre>c:\\>grep \"VERSION_MAJOR\" version.h | sed -e \"s\/^.* VERSION_MAJOR\\s*\\([0-9]\\+\\)\/\\1\/g\"\r\n3<\/pre>\n<\/blockquote>\n<p>So now we have extracted the version number corresponding to &#8220;VERSION_MAJOR&#8221;.  However, in order to conditionally branch in our batch script, we need to get this value into an environmental variable.  In bash, this would be accomplished with backticks.  As it turns out, Windows batch files <b>do<\/b> provide similar functionality, but through a much clumsier means:<\/p>\n<blockquote>\n<pre>c:\\>for \/f \"tokens=3\" %i in ('grep \" VERSION_MAJOR\" version.h') do set MAJORVERSION=%i\r\nc:\\>echo %MAJORVERSION%\r\n3<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>We have a version.h file. It contains lines such as: #define VERSION_MAJOR 3 #define VERSION_MINOR 1 By piping this through grep, I can get the line I&#8217;m looking for: c:\\>grep &#8220;VERSION_MAJOR&#8221; version.h #define VERSION_MAJOR 3 But what I really want &hellip; <a href=\"https:\/\/www.curlybrace.com\/words\/2008\/07\/extracting-a-version-number\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[193,15],"tags":[],"class_list":["post-146","post","type-post","status-publish","format-standard","hentry","category-scripting","category-technology"],"_links":{"self":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":4,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":400,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/146\/revisions\/400"}],"wp:attachment":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}