Code for extracting from the right side with cut command?

vfclists picture vfclists · Oct 12, 2012 · Viewed 10.1k times · Source

There is a sample of using the cut command to extract parts of a string starting from the left. An example is given below.

$ echo "abc-def-ghi-jkl" | cut -d- -f-2
abc-def

How can the same code be adapted to extracting from the right side? I thought of reversing the words and applying the same method, but it was too complicated.

Answer

iruvar picture iruvar · Oct 12, 2012

you could use rev

echo "abc-def-ghi-jkl" | rev | cut -d- -f-2 | rev