How to get second last field from a cut command

Archit Jain picture Archit Jain · Jul 14, 2013 · Viewed 64.4k times · Source

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 ?

example input

text,blah,blaah,foo
this,is,another,text,line

expected output

blaah
text

Answer

Archit Jain picture Archit Jain · Jul 14, 2013

Got a hint from Unix cut except last two tokens and able to figure out the answer :

cat datafile | rev | cut -d '/' -f 2 | rev