In bash how to get the field before the last one?
Parse field before last in bash shell script – An interesting piece of code today which could hit any system administrator who handles a lot of data parsing and processing, is how to extract some data from a comma seperated file when one of the data fields contains a comma as well….
If you have tried this yourself you will know that the output you were hoping for will be screwed. As an example here is some data –
doe,john,9 london road,"neighbour jack jones",2016,semi detached
jones,jack,10 london road,"neighbour smith,jane",2016,semi detached
As you can see the neighbour field on the 2nd record has listed jane smith with a comma in the data (even though it has quotes around it bash cut ignores that) making it hard for you to do a simple cut to get the date moved in (2016 – the 2nd to last field)
[root tmp]# cat /tmp/m5 | cut -d"," -f5
2016
jane
[root tmp]#
So… how to get around it and parse field before last you ask?
Well a command called ‘rev’ will do it for you! Lets first show you what it does –
[root tmp]# cat /tmp/m5 | rev
dehcated imes,6102,senoj kcaj ruobhgien,daor nodnol 9,nhoj,eod
dehcated imes,6102,enaj,htims ruobhgien,daor nodnol 01,kcaj,senoj
[root tmp]#
As you can see its simply reversed everything on each line character by character. but now you can see that the date you want is in field 2, so lets grab it –
[root tmp]# cat /tmp/m5 | rev | cut -d"," -f2
6102
6102
[root tmp]#
And then we need to put it back in the right sequence by reversing it again back to its original form –
[root tmp]# cat /tmp/m5 | rev | cut -d"," -f2 | rev
2016
2016
[root tmp]#
Nothing in bash is hard.. whats hard is knowing the commands in order to do it!
If you enjoyed this post or found it useful then please share it to your friends or social networks. Also check out more bash tips around the bashworkz website.