How to split a string using Awk

Using Awk a string may be split at least using two different methods.

Let’s split the strings given in file foo. The file consists of 5 columns separated by a comma (,) so we will split the lines at “,”.

awk ‘{split($0,array,”,”)}{print array[1],array[2],array[3],array[4],array[5]} foo

awk -F “,” ‘{print $1,$2,$3,$4,$5}’ foo

Where foo is a file such as this e.g:
1005.709115,-3.974680,0.081765,3.265020,Wed 23 Nov 2011 11:27:14 AM UTC
1006.709004,-3.974761,0.084558,3.265004,Wed 23 Nov 2011 11:27:15 AM UTC
1007.708987,-3.973002,0.087218,3.265001,Wed 23 Nov 2011 11:27:16 AM UTC
1008.709055,-3.969182,0.089719,3.265034,Wed 23 Nov 2011 11:27:17 AM UTC

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


This blog is kept spam free by WP-SpamFree.