1833995 Members
2890 Online
110063 Solutions
New Discussion

Re: finding date in wtmp

 
SOLVED
Go to solution
Dalin Bruns
Occasional Advisor

finding date in wtmp

I want to search for a calculated date in a converted wtmp file, but am not able to get extra space in single digit dates in order to match the format in the converted wtmp.

e.g. excerpt from line in wtmp ... Jun 6
Note, the extra space between month and 6. When I calculate a date, I can't get the extra space in there. Please help.
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: finding date in wtmp

Hi:

How do you want to calculate the date and how do you want to search?

Regards!

...JRF...
Sridhar Bhaskarla
Honored Contributor

Re: finding date in wtmp

Hi,

Try this way

DAY=$(date +%d)
MONTH=$(date +%b)
DATE=$(printf "%s %2.3s" $MONTH $DAY)
last -R |grep "$DATE"


The above is only example. You can use that logic in your script.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Dalin Bruns
Occasional Advisor

Re: finding date in wtmp

interesting ... the extra space got taken out of my example.

Here's what I want to do. I want to keep the last 90 days of data in my wtmp, deleting the rest. I have calculated 90 days prior to today. My next step was going to be determining the line number matching my calc'd date and delete all previous lines. Something along these lines ...

X=grep -n "Jun 6" wtmp.tmp | head -1 | cut -d : -f 1

sed '1,Xd' wtmp.tmp > wtmp.clean

I know that isn't all correct, but I'm new to this scripting thing (coming from Win env) and I haven't worked out all the details. Any help would be greatly appreciated.
James R. Ferguson
Acclaimed Contributor

Re: finding date in wtmp

Hi Dalin:

Your approach is fine. Do this:

X='Jun 6' #...there are really 2-blanks here...

# sed -e "1,/$X/"d filein

Regards!

...JRF...
Dalin Bruns
Occasional Advisor

Re: finding date in wtmp

Thanks guys ... couldn't get Sri's to show extra space on my system after modifying date line to calculate back enough days to get in single digits, but it was a new approach ... James thanks for pointing out how to avoid all of that line number stuff. But, I still have the extra space issue.

I'm now thinking that I may have to awk out the month and day from the appropriate columns of the wtmp.tmp file and then match on both.

WDYT?
Sridhar Bhaskarla
Honored Contributor
Solution

Re: finding date in wtmp

Hi Dalin,

If you look at the value of $DATE, it will have two spaces for a single digit date and one space for double digit date from the month's field.

If you already calculated the date, it is easy to create an extra space between the month and the day using printf statement but you have to first devide the date into day and month as I did in the example.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
James R. Ferguson
Acclaimed Contributor

Re: finding date in wtmp

Hi (again):

If you folllow Sri's use of 'printf' you can construct the "$X" argument to the 'sed' I suggested.

Regards!

...JRF...
Rodney Hills
Honored Contributor

Re: finding date in wtmp

Within ksh you can use typeset to force a variable to be two characters (with blank padding).

typeset -R2 daynum=6

Then when you use $daynum in your script it will have the extra space.

HTH

-- Rod Hills
There be dragons...
Dalin Bruns
Occasional Advisor

Re: finding date in wtmp

I tried again and it works!! Thanks for your suggestions. I am now well down the road to cleaning up my wtmp files.

Dalin