<p>I have a filename in a format like:</p>

<blockquote>
  <p><code>system-source-yyyymmdd.dat</code></p>
</blockquote>

<p>I'd like to be able to parse out the different bits of the filename using the "-" as a delimiter.</p>

[{'answer_id': 49406, 'author': 'David', 'author_id': 381, 'author_profile': 'https://Stackoverflow.com/users/381', 'pm_score': 3, 'selected': False, 'text': '<p>Use the <code>cut</code> command.</p>\n\n<p>e.g.</p>\n\n<pre><code>echo "system-source-yyyymmdd.dat" | cut -f1 -d\'-\'\n</code></pre>\n\n<p>will extract the first bit.</p>\n\n<p>Change the value of the <code>-f</code> parameter to get the appropriate parts.</p>\n\n<p>Here\'s a guide on the <a href="http://learnlinux.tsf.org.za/courses/build/shell-scripting/ch03s04.html" rel="nofollow noreferrer">Cut</a> command.</p>\n'}, {'answer_id': 49409, 'author': 'Bobby Jack', 'author_id': 5058, 'author_profile': 'https://Stackoverflow.com/users/5058', 'pm_score': 6, 'selected': True, 'text': '<p>You can use the <a href="http://en.wikipedia.org/wiki/Cut_(Unix)" rel="noreferrer">cut command</a> to get at each of the 3 \'fields\', e.g.:</p>\n\n<pre><code>$ echo "system-source-yyyymmdd.dat" | cut -d\'-\' -f2\nsource\n</code></pre>\n\n<p>"-d" specifies the delimiter, "-f" specifies the number of the field you require</p>\n'}, {'answer_id': 49482, 'author': 'flight', 'author_id': 3377, 'author_profile': 'https://Stackoverflow.com/users/3377', 'pm_score': 3, 'selected': False, 'text': '<p>Depending on your needs, <a href="http://en.wikipedia.org/wiki/AWK" rel="noreferrer">awk</a> is more flexible than cut. A first teaser:</p>\n\n<pre><code># echo "system-source-yyyymmdd.dat" \\\n    |awk -F- \'{printf "System: %s\\nSource: %s\\nYear: %s\\nMonth: %s\\nDay: %s\\n",\n              $1,$2,substr($3,1,4),substr($3,5,2),substr($3,7,2)}\'\nSystem: system\nSource: source\nYear: yyyy\nMonth: mm\nDay: dd\n</code></pre>\n\n<p>Problem is that describing awk as \'more flexible\' is certainly like calling the iPhone an enhanced cell phone ;-)</p>\n'}, {'answer_id': 110635, 'author': 'Shannon Nelson', 'author_id': 14450, 'author_profile': 'https://Stackoverflow.com/users/14450', 'pm_score': 1, 'selected': False, 'text': '<p>Another method is to use the shell\'s internal parsing tools, which avoids the cost of creating child processes:</p>\n\n<pre>\noIFS=$IFS\nIFS=-\nfile="system-source-yyyymmdd.dat"\nset $file\nIFS=$oIFS\necho "Source is $2"\n</pre>\n'}, {'answer_id': 481899, 'author': 'Colas Nahaboo', 'author_id': 58468, 'author_profile': 'https://Stackoverflow.com/users/58468', 'pm_score': 3, 'selected': False, 'text': '<p>A nice and elegant (in my mind :-) using only built-ins is to put it into an array</p>\n\n<pre><code>var=\'system-source-yyyymmdd.dat\'\nparts=(${var//-/ })\n</code></pre>\n\n<p>Then, you can find the parts in the array...</p>\n\n<pre><code>echo ${parts[0]}  ==&gt; system\necho ${parts[1]}  ==&gt; source\necho ${parts[2]}  ==&gt; yyyymmdd.dat\n</code></pre>\n\n<p>Caveat: this will not work if the filename contains "strange" characters such as space, or, heaven forbids, quotes, backquotes...</p>\n'}, {'answer_id': 51213978, 'author': 'William Pursell', 'author_id': 140750, 'author_profile': 'https://Stackoverflow.com/users/140750', 'pm_score': 0, 'selected': False, 'text': '<p>The simplest (and IMO best way) to do this is simply to use <code>read</code>:</p>\n\n<pre><code>$ IFS=-. read system source date ext &lt;&lt; EOF\n&gt; foo-bar-yyyymmdd.dat\n&gt; EOF\n$ echo $system\nfoo\n$ echo $source $date $ext\nbar yyyymmdd dat\n</code></pre>\n\n<p>There are many variations on that theme, many of which are shell dependent:</p>\n\n<p><code>bash$ IFS=-. read system source date ext &lt;&lt;&lt; foo-bar-yyyymmdd.dat</code></p>\n\n<pre><code>echo "$name" | { IFS=-. read system source date ext\n   echo In all shells, the variables are set here...; }\necho but only in some shells do they retain their value here\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/49403', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4003/']