I have a set of data as input and need the second last field based on deleimiter. The lines may have different numbers of delimiter. How can I get second last field ?
Code for GNU sed:
$ echo text,blah,blaah,foo|sed -r 's/^(\S+,){2}(\S+),.*/\2/' blaah $ echo this,is,another,text,line|sed -r 's/^(\S+,){2}(\S+),.*/\2/' text
Code example similar to sudo_O's awk
code:
$ sed -r 's/.*,(\w+),\w+$/\1/' file blaah text
It might be better to use more specialised programs for CSV
files, eg. awk or excel.