1833704 Members
2804 Online
110062 Solutions
New Discussion

please help

 
SOLVED
Go to solution
Ken Lee_1
Occasional Contributor

please help

I am doing a script which I want to grep the last username of a file. Assuming below is a list of username which contained in a file with delimiter ":". (It is continuous line) With what command so that I can get the last username "mutvomo".

labdaulrr:lsaariunb:crupafng:ltafmbuna:mrginilas:mthgerlas:mlutthers:maeskum:mutvomo:
5 REPLIES 5
Vincent Farrugia
Honored Contributor

Re: please help

Hi,

If the sentence is of fixed length, you can use the cut command as follows:

cut -f10 -d:

Replace 10 with the number of the field you wish, e.g. if the last field is the 15th field, replace 10 by 15.

Hope it helps,
Vince
Tape Drives RULE!!!
Ken Lee_1
Occasional Contributor

Re: please help

The length of the file is not fixed. What should I do now?
Vincent Farrugia
Honored Contributor

Re: please help

Hi,

You can count the number of : in the file first, that assumes that the result is the numbner of usernames resent in the file.

This would be done using wc

usernames=$(wc -c :)

cut -f$usernames -d:

Dunno whether it works like this, we don't have power on the servers at the moment, so I didn't have time to check the syntax. It should be something like that though.

Hope it helps,
Vince
Tape Drives RULE!!!
Vincent Farrugia
Honored Contributor

Re: please help

Sorry, the wc command doesn't work like that...

I'll have to come up with something else.

SORRY
Tape Drives RULE!!!
Satish Y
Trusted Contributor
Solution

Re: please help

Try this,

awk -F":" '{print $(NF-1)}' myfile

Cheers...
Satish.
Difference between good and the best is only a little effort