Cut the last field

Hello guys.

Is there any way I can cut the last field using "cut" ???

(without putting it into a while...)

Thanks.
435 Gavea.

your question is kinda vague.

what are you trying to do that you want to avoid a while loop?

not sure how you would use the cut
but you can try this
awk '{print $NF}'

instead of cut

so if you have

line="hhh kkk lll iii ddd kkke"
then
echo $line|awk '{print $NF}'

will give you kkke

give a sample input and output, so that we can help you understand even better

You didn't like the answer you got last time? :stuck_out_tongue:

Does the awk solution works with different delimiters ?

I was talking about something like this:

abcd/efgh/ijklm/nopq

And I'd like to "cut" only the nopq

you can specify the delimiter using the -F option

awk -F/ '{.....

try to use the man pages in case you need any information of a unix command

awk by itself is a very powerful tool. Even if your purpose is served at present I would recommend you to go through the man page of the awk filter

Besides if you search the forum for awk, you will get lotsa information

happy awking :wink:

I can also recommend the manual pages for the dirname and basename commands.

Using cut you can only select fields from the left to the right. If you don't know how many fields you will have after the cut then you will not be able to get the last field.
But there is a solution for this using rev. Reverse the string, cut and get the first field and reverse it back. So in your example "abcd/efgh/ijklm/nopq":

echo "abcd/efgh/ijklm/nopq" | rev | cut -d/ -f1 | rev

the result will be "nopq"

gppcj

Using cut you can only select fields from the left to the right. If you don't know how many fields you will have after the cut then you will not be able to get the last field.
But there is a solution for this using rev. Reverse the string, cut and get the first field and reverse it back. So in your example "abcd/efgh/ijklm/nopq":

echo "abcd/efgh/ijklm/nopq" | rev | cut -d/ -f1 | rev

the result will be "nopq"

gppcj

If you have the "rev" command, use gppcj solution.
Otherwise, use awk, because with cut, you must know the number of fields to get the last one.
If the delimiter is a / you can use "basename" command.