Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FInger and cut (sounds painful doesn't it) 1

Status
Not open for further replies.

GirlInTrouble

Technical User
May 27, 2004
12
GB
Hi there you guys ;-)

Im trying to get the first 3 fields from a finger -s command
I've tried using cut but that doesn't work.

who |cut -d" " -f1|xargs finger -s|cut -d" " -f1,2,3

The output of finger is seperated with multiple spaces. Do I used IFS again? How do I say my delimiter is junk? I tried -s on cut - no use though. I also tried pr (from last time ;-) )
Any ideas boys?

Thanks
 
How about:
Code:
who|awk '{print $1}'|xargs finger -s|tail +2|awk '{print $1,$2,$3}'
Cheers, Neil
 
who |cut -d" " -f1|xargs finger -s|awk '{print $1,$2,$3}'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi Toolkit
Oh my God I've been laughing because I've never seen this tail +2, man page here I come.

It works except for real users (ie. non root) it gives me thier login name, first name and second name, but not their tty , (which I DO get for root)
which I expecially want as it is prefixed with a * to show if they are excepting messages. I guess the input isn't consistant (PS I'm on Solaris)

 
Just one more option using the tr command to squeeze out the spaces.

The first set of single quotes contain a tab and a space, followed by two spaces in the next set of single quotes.

Code:
finger -s | tr -s '        ' ' '|cut -d' ' -f1-3
 
The tail plus option tells the tail command to start at the line number given to the bottom of the file in question.

Basically it is used to strip uneeded header information as in the ls -l command and the column headings from the finger command.


Cheers,

Steve
 
If you're not interested in their real name (which may or may not contain spaces which will screw up cut and awk), then try:
Code:
who|awk '{print $1}'|xargs finger -s -w|awk '{printf "%-20s %-20s\n", $1, $2}'
Cheers, Neil
 
Heres an example of what I get:

Login Name TTY
root Super-User console
root Super-User pts/2
root Super-User console
root Super-User pts/2
joe Joe Bloggs

I het joes last name not his tty... I want also want the * on the tty which says he's not accepting messages.


 
I guess it's probably easier to get the full name from the passwd file, it just seems to be a little uncessacary as I'm already looking at finger.
 
'Super-User' doesn't contain a space, whereas 'Joe Bloggs' does, so the field position on the line is not consistent. Something like the following will work for you:
Code:
who|awk '{print $1}'|xargs finger -s|tail +2|sed -e 's/-/ /'|awk '{print $1,$2,$3,$4}'
Cheers, Neil
 
or:

who |cut -d" " -f1|xargs finger -s | sed -e 's/[ ][ ][ ]*/|/g' | awk -F'|' '{print $1,$2,$3}'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
OK, I get it, if it doesn't look how you want you MAKE IT look like you want!

Ok so I'm trying to make it look nice and I have broken it!
I seem to have more lines comming back than user sessions %)

who |awk '{print $1}'|xargs finger -s|tail +2|sed -e 's/-/ /'|awk '{printf "%10s %10s %-8s %8s\n", $1,$2,$3,$4}'

IS it the tail +2? I'm not sure what happened, the only bit I don't understand is the tail+2
 
It looks like the who and the finger together are overkill, it seems to be resulting in duplicate lines. I've chopped off the who from the from the front (to the finger - even worse amputation) , now I get less lines than tty sessions. Perhaps theres a flag on finger...I'm gonna keep at it.
 
Hey there what happened to you guys, did you wimp out or did you just think I was being lame ;-) I probably was, I had to finish this in a hurry. Anyhow just to let you know, I might be slow but not stupid, so here's my answer:
PS I'm brand new to awk and sed and had to scratch my head but I had to use "sort|uniq" to get it to work, so for posterity here it is:

who |awk '{print $1}'|sort|uniq|xargs finger -s|tail +2|sed -e 's/-/ /'|awk '{printf "%-8s %-8s %10s %-10s\n", $1,$4,$2,$3}'

Its still a compromise, works badly with very long gcos fields because finger -s can't handle it and I wanted the * in a seperate column, but you know what they say about beggars.

Couldn't have done it without your help!!! You're all smashing. So thanks as ever
A
 
Have I missing something or this simple code suffice ?
finger -s|awk 'NR==1{lg=index($0,"Idle")-2;next}{print substr($0,1,lg)}'

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
GirlInTrouble,
If you post some sample output from the following, perhaps we could see how best to manipulate your data:
Code:
who -q | head -1 | xargs finger -s | sort -u
Cheers, Neil
 
Or even:
Code:
who -q | grep -v "#" | xargs finger -s | sort -u
Cheers, Neil
 
Hi Neil, determined aren't you (thanks)
Hope this isn't too much info for you. At least you can get a better feel for what I'm TRYING to do ;-)
BTW IT IS actually slightly better formatted than you can see here.

This is what I get from your 2nd command
who -q | grep -v "#" | xargs finger -s | sort -u
aaaaaaaa aaaaaaaaa bbbbbbbbbb pts/4 2 Wed 13:07 GOOSE
aisha Aisha Xxxxxx pts/0 Wed 13:06 GOOSE
Login Name TTY Idle When Where
robin Robin Xxxxxxx *pts/3 2 Wed 13:07 GOOSE
root Super-User console Wed 13:09 :0
root Super-User pts/2 Wed 13:09 :0.0

sendmesg () {
IFS="
"
PS3="${REVERSE}Select the user you want to send a message to:${OFF} "
echo "\n${BOLD}Note${OFF}: The 2nd field shows the terminal, if preceded by an asterisk \"*\", the user may not be accepting messages\n"
echo "Users Currently logged in are: \n"
select USER in `who |awk '{print $1}'|sort|uniq|xargs finger -s|tail +2| sed -e 's/-/ /'|awk '{printf "%-8s %-8s %10s %-10s\n", $1,$4,$2,$3}'` Exit
do
TTY=`echo $USER|cut -c10-17`
REJECT=`echo $USER|cut -c10`
USER=`echo $USER|cut -c1-8`
case $USER in
Exit) PS3="${REVERSE}Your current directory is $(pwd), Enter your menu choice:${OFF} "
return;;
*) read MESG?"Please enter a short message: ";
if eval write $USER "$TTY" <<-END
$MESG
END
then
echo "Your message was successfully sent\n"
else
echo "Your message could not be delivered, the user may not be accepting messages. Sorry\n"
fi
PS3="${REVERSE}Your current directory is $(pwd), Enter your menu choice:${OFF} "
return;;
esac
done
}

This is the output, nt exactly what I want becasue I can't actually see how to set up $REJECT and do anything with it in the select list, obviously I can do something with it later. I would have actually liked to have it as a seperate column but the way I'm awk-printing it alters the column

Note: The 2nd field shows the terminal, if preceded by an asterisk "*", the user may not be accepting messages

Users Currently logged in are:

1) aaaaaaaa pts/4 aaaaaaaaa bbbbbbbbbb
2) aisha pts/0 Aisha Xxxxxx
3) robin *pts/3 Robin Xxxxxxx
4) root console Super User
5) root pts/2 Super User
6) Exit
Select the user you want to send a message to:
 
You could try replacing your select line with something like:
Code:
select USER in `who -q | grep -v "#" | xargs finger -s | tail +2 | sort -u | perl -e '
while (<>) {
    if (m#^(\S+)\s+(.+)\s+(\*?)(pts/\d+|console)#) {
        write;
    }
}

format STDOUT=
@<<<<<<<< @<<<<<<<<<<<<<<<<<<<<< @<<<< @<<<<<<<<<<<<<
$1,$2,$3,$4
.
''
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top