Shell – How to Cut a Field from Text Line Counting from the End

cutshelltext processing

I know how to select a field from a line using the cut command. For instance, given the following data:

a,b,c,d,e
f,g,h,i,j
k,l,m,n,o

This command:

cut -d, -f2 # returns the second field of the input line

Returns:

b
g
l

My question: How can I select the second field counting from the end? In the previous example, the result would be:

d
i
n

Best Answer

Reverse the input before and after cut with rev:

<infile rev | cut -d, -f2 | rev

Output:

d
i
n
Related Question