Unix cut except last two tokens Unix cut except last two tokens bash bash

Unix cut except last two tokens


With cut:

$ echo t1_t2_t3_tn1_tn2.sh | rev | cut -d_ -f3- | revt1_t2_t3

rev reverses each line.The 3- in -f3- means from the 3rd field to the end of the line (which is the beginning of the line through the third-to-last field in the unreversed text).


You may use POSIX defined parameter substitution:

$ name="t1_t2_t3_tn1_tn2.sh"$ name=${name%_*_*}$ echo $namet1_t2_t3


It can not be done with cut, However, you can use sed

sed -r 's/(_[^_]+){2}$//g'