Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code for extracting from the right side with cut command?

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.

like image 295
vfclists Avatar asked Oct 12 '12 13:10

vfclists


1 Answers

you could use rev

echo "abc-def-ghi-jkl" | rev | cut -d- -f-2 | rev
like image 123
iruvar Avatar answered Oct 12 '22 06:10

iruvar