[Solved] Finger command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Finger command
# 8  
Old 02-17-2014
It looks like you can already produce your report just using finger, but need to add the year to lines that have a timestamp. Basically you have four line formats that you need to examine:
Code:
Last login Wed Aug  7, 2013 on pts/111
Last login Mon Sep 23 17:28 on pts/47 from remb02mkiwsf25
On since Feb 17 15:26:40 on pts/75
On since Jul 25, 2013 on pts/12

You don't need to do anything to the lines that have a year in them already (the 1st and 4th lines shown above). For the 2nd and 3rd lines, you can insert the current or previous year before the timestamp (or replace the timestamp) depending on whether the given month is less than the current month in this year. So, if the report you generated (as shown in message #3 in this thread) is stored in a file name report.txt, the following shell and awk script:
Code:
#!/bin/ksh
cmy=$(date +'%m%Y')
cm=${cmy%????}
cy=${cmy#??}
awk -v cm="${cm# }" -v cy="$cy" '
# Set the year associated with the month (M) and timestamp or year (TorY):
BEGIN { ML = "JanFebMarAprMayJunJulAugSepOctNovDec"
}
function setyear(M, TorY) {
        if(index(TorY, ":") == 0) return(TorY)  # We have a year; not a time.
        m = (index(ML, M) + 2) / 3              # Convert month name to number
        if(m > cm)      return(cy - 1 " " TorY) # return last year + time
        else            return(cy " " TorY)     # return this eyar + time
}
/^On since/ {
        $5 = setyear($3, $5)
}
/^Last login/ {
        $6 = setyear($4, $6)
}
{       print
}' report.txt

will produce the output:
Code:
The below user id wre34 is not an active user from Sep 2013. his last logon Aug 7 2013
  
Directory: /export/home/wre34           Shell: /bin/ksh
Last login Wed Aug 7, 2013 on pts/111
No unread mail
No Plan.
  
The other user id ivv41 las logon was Sep 21 2013
   
Directory: /export/home/ivv41           Shell: /bin/ksh
Last login Mon Sep 23 2013 17:28 on pts/47 from remb02mkiwsf25
Mail last read Sat Sep 21 15:13:31 2013
No Plan
    
current usr id
     
Directory: /export/home/por12           Shell: /bin/ksh
On since Feb 17 2014 15:26:40 on pts/75
Mail last read Sat Feb  1 15:25:28 2014
No Plan

If you just want the year and not the timestamp for data within the last six months, delete the code marked in red.

Although this was written and tested using the Korn shell, it will also work with bash or any other shell that performs standard parameter substitutions.

If you want to run this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of the default /usr/bin/awk.
# 9  
Old 02-17-2014
Thank you so much Don.

I tired the code you suggested. But if
Code:
if(m > cm)      return(cy - 1 " " TorY) # return last year + time

if user last Jan 11 2014 , but the above code will return Jan 11 2013. So it will not meet the requirment.
# 10  
Old 02-17-2014
did you change your awk to /usr/xpg4/bin/awk?
I just tested on a solaris9 and the given code by Don works perfectly... ( at the condition as mentionned in post#8 to to use a different awk for Solaris...)
# 11  
Old 02-17-2014
Quote:
Originally Posted by stew
Thank you so much Don.

I tired the code you suggested. But if
Code:
if(m > cm)      return(cy - 1 " " TorY) # return last year + time

if user last Jan 11 2014 , but the above code will return Jan 11 2013. So it will not meet the requirment.
What OS and shell are you using?
Please show us an exact line of input (in CODE tags) for which the code I provided changed something like:
Code:
Last login Thu Jan 23 17:28 on pts/47 from remb02mkiwsf25
On since Jan 17 15:26:40 on pts/75

to:
Code:
Last login Thu Jan 23 2013 17:28 on pts/47 from remb02mkiwsf25
On since Jan 17 2013 15:26:40 on pts/75

The output I get when feeding the above lines into that script is:
Code:
Last login Thu Jan 23 2014 17:28 on pts/47 from remb02mkiwsf25
On since Jan 17 2014 15:26:40 on pts/75

# 12  
Old 02-17-2014
I have written

Code:
#!/bin/ksh
cmy=$(date +'%m%Y')
cm=${cmy%????}
cy=${cmy#??}
/usr/xpg4/bin/awk  -v cm="${cm# }" -v cy="${cy}" 'BEGIN{ ML = "JanFebMarAprMayJu
nJulAugSepOctNovDec"}'
function setyear(M, TorY) {
        if(index(TorY, ":") == 0) return(TorY)  # We have a year; not a time.
        m = (index(ML, M) + 2) / 3              # Convert month name to number
        if(m > cm)      return(cy - 1 " " TorY) # return last year + time
        else            return(cy " " TorY)     # return this eyar + time
}
/^On since/ {
        $5 = setyear($3, $5)
}
/^Last login/ {
        $6 = setyear($4, $6)
}
{       print
}' report.txt


but I am getting below error

Code:
report.ksh[8]: syntax error at line 8 : `(' unexpected

# 13  
Old 02-17-2014
Quote:
Originally Posted by stew
I have written

Code:
#!/bin/ksh
cmy=$(date +'%m%Y')
cm=${cmy%????}
cy=${cmy#??}
/usr/xpg4/bin/awk  -v cm="${cm# }" -v cy="${cy}" 'BEGIN{ ML = "JanFebMarAprMayJu
nJulAugSepOctNovDec"}'
function setyear(M, TorY) {
        if(index(TorY, ":") == 0) return(TorY)  # We have a year; not a time.
        m = (index(ML, M) + 2) / 3              # Convert month name to number
        if(m > cm)      return(cy - 1 " " TorY) # return last year + time
        else            return(cy " " TorY)     # return this eyar + time
}
/^On since/ {
        $5 = setyear($3, $5)
}
/^Last login/ {
        $6 = setyear($4, $6)
}
{       print
}' report.txt


but I am getting below error

Code:
report.ksh[8]: syntax error at line 8 : `(' unexpected

Adding an unmatched single quote in the middle of an awk script can cause lots of syntax problems (such as the error you are getting here).

Spliting the string containing the abbreviated month names onto multiple lines will cause other problems depending on the current month and the months of the dates found in the data you're processing.

Try replacing the lines in the code you have written:
Code:
/usr/xpg4/bin/awk  -v cm="${cm# }" -v cy="${cy}" 'BEGIN{ ML = "JanFebMarAprMayJu
nJulAugSepOctNovDec"}'

with something closer to the code I suggested:
Code:
/usr/xpg4/bin/awk  -v cm="${cm# }" -v cy="${cy}" '
BEGIN{  ML = "JanFebMarAprMayJunJulAugSepOctNovDec"
}

This User Gave Thanks to Don Cragun For This Post:
# 14  
Old 02-18-2014
Thanks Don. It worked. But I am getting the below O/P when I ran the script

Code:
+ + date +%m%Y
cmy=022014
+ cm=02
+ cy=2014
+ /usr/xpg4/bin/awk -v cm=02 -v cy=2014 BEGIN{ ML = "JanFebMarAprMayJunJulAugSepOctNovDec"}
function setyear(TorY,M) {
        if(index(TorY, ":") == 0) return(TorY)  # We have a year; not a time.
        m = (index(ML, M) + 2) / 3              # Convert month name to number
        if(m > cm)      return(cy - 1 " " TorY) # return last year + time
        else            return(cy " " TorY)     # return this eyar + time
}
/^On since/ {
        $5 = setyear($3, $5)
}
/^Last login/ {
        $6 = setyear($4, $6)
}
{       print
} report.txt
st login Wed Aug  7, 2013 on pts/111
Last login Mon Sep 23 Sep on pts/47 from remb02mkiwsf25
On since Feb 17 Feb on pts/75
On since Jul 25, Jul on pts/12

---------- Post updated at 11:16 PM ---------- Previous update was at 11:14 PM ----------

The year is not printing for the last three lines

Code:
st login Wed Aug  7, 2013 on pts/111
Last login Mon Sep 23 Sep on pts/47 from remb02mkiwsf25
On since Feb 17 Feb on pts/75
On since Jul 25, Jul on pts/12

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Not able to disable finger & telnet command in Solaris 8

Hi I need to disable finger & telnet command in solaris 8 I have put the # infront of finger and telnet line in /etc/inetd.conf file. Further I have run the below command kill -1 <process id of inetd > But when I am running finger command it is till giving information for remote machine... (8 Replies)
Discussion started by: amity
8 Replies

2. UNIX for Beginners Questions & Answers

Finger command and security issue

$ finger yeti Login: yeti Name: yeti Directory: /arpa/tz/y/yeti Shell: /bin/ksh On since Wed Apr 2 15:24 (UTC) on pts/149 Mail last read Mon Mar 31 11:08 2014 (UTC) No Plan. Hi there, I am trying to... (2 Replies)
Discussion started by: alvinoo
2 Replies

3. UNIX for Dummies Questions & Answers

Extracting specific info finger command

how to extract user machine name for current terminal using finger command below command gives machinename for all session , is it possible to filter it to only currernt terminal ? finger -b -p $LOGNAME | grep from (12 Replies)
Discussion started by: lalitpct
12 Replies

4. UNIX for Dummies Questions & Answers

Finger command help

Hi Does anyone know if there is anyway of doing the finger command for all user id's in my enviroment. What I need to obtain is the full names of all users on the system. I know if i do the finger command with no arguments it will list users currently logged in, but i need all users... ... (2 Replies)
Discussion started by: m3y
2 Replies

5. UNIX for Advanced & Expert Users

finger command

I want to know the correct version of how i should use the finger command in this example below.(os is debian lenny) (nymserver.pl is located in /home/nymserv directory.) the two versions are : (in/etc/inetd.conf) finger stream tcp nowait nymuser /usr/nym/nymserv nymserv... (3 Replies)
Discussion started by: xstation
3 Replies

6. UNIX for Dummies Questions & Answers

Extracting specific info from finger command

Hello all, my unix is bash based and the finger command output is: Login Name Tty Idle LoginTime Office amos.john Amos John pts/26 1 Dec 5 16:18 (77.100.22.07) What am trying to achieve is extract the Login (amos.john) and Name (Amos John) from this output without using awk or sed. ... (1 Reply)
Discussion started by: franny
1 Replies

7. Shell Programming and Scripting

help in finger command.

Hi, iam using sunsolaris. when you type finger command -- it dispalys information about local and remote users. but here it shows as can't stat /dev/gold:8664 can anybody help what is the solution for this error. previously the output came. thanks, shan (1 Reply)
Discussion started by: shanshine
1 Replies

8. UNIX for Advanced & Expert Users

finger command

Hello all, Here is what I am trying to do. If a user exist, then send an echo "EXIST" or else "DOES NOT EXIST". (under HP-UX) Kind of: #!/usr/bin/sh USER=mylogin finger $USER if $? = 0 then echo "EXIST"" else echo "DOES NOT EXIST" fi (10 Replies)
Discussion started by: qfwfq
10 Replies

9. Shell Programming and Scripting

How to input username on text file into finger command on shell script

I'm trying to clean up my server and I have the list of some "special" users stored on the text file like this Now I want to write a shell script to finger all of them so I can have some kind of ideas who they are but here comes the problem....I completely forgot how to do it with shell... (3 Replies)
Discussion started by: Micz
3 Replies

10. Shell Programming and Scripting

Using the Finger command in a Script

This is my senario..... The user enters a userid into linux. ((I have have already scripted the command to read this userid.)) I need help in writing the script so It reads the userID and in conjuction w/ the finger command displays to the user "no plan" on the screen (so the user reads/sees... (4 Replies)
Discussion started by: apolishuk
4 Replies
Login or Register to Ask a Question