Operating System - HP-UX
1835270 Members
2573 Online
110078 Solutions
New Discussion

Need help with a script (fixing some formatting)

 
SOLVED
Go to solution
MAD_2
Super Advisor

Need help with a script (fixing some formatting)

I am building a script that extracts information from the commands "last -R" and "lastb -R"

which will be used to create pipe separated files that will later be imported into Oracle

database tables. This is to facilitate reading and viewing to some managers who want to

look at system access info using some interface repots through access or something like

that.

Well, most of the the script's syntax I am using is included in my next posting as an

attachment. Where I need help is with the formatting that takes place with what
I get with "lastb -R", attached is a detailed explanation of my dilemma.
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
8 REPLIES 8
MAD_2
Super Advisor

Re: Need help with a script (fixing some formatting)

Let me add the script now...

Oh, and let me toss another question, you may see the vi command I used in the script. What I was trying to do there was eliminate the very last line of the of the files because it was coming out blank and I did not need it. How can I remove the last line of a file (no matter how many lines it has and I won't know -- well, I could know using wc?)
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
RAC_1
Honored Contributor

Re: Need help with a script (fixing some formatting)

grep -v "^$" file
will exclude all blank line.

You script is too big to read for me.
There is no substitute to HARDWORK
John Poff
Honored Contributor

Re: Need help with a script (fixing some formatting)

Hi,

I think you can take care of your lastb problem a couple of different ways. Take a look at the 'cut' command. You can specify columns for cutting with that command so you should be able to specify a field. Or, you could use awk and the substr function to break out the strings. Probably the best way to do it would be with Perl. I think there might even be a Perl module that reads the last and lastb information from wtmp/btmp for you.

JP
James R. Ferguson
Acclaimed Contributor

Re: Need help with a script (fixing some formatting)

Hi:

To eliminate the last line of the file, do this:

# sed -e '$d' infile > outfile

As far as the login name containing blanks, I'd suggest you unconditionally treat the extent of the first field as 8-characters (which should be the maximum for a login name) and then proceed to reformat with pipe characters.

Regards!

...JRF...
Sridhar Bhaskarla
Honored Contributor

Re: Need help with a script (fixing some formatting)

Hi,

Below is a simple script that does a lastb -R and outputs into result as a comma seperated file.

lastb -R |sed 's/ /#/g' |while read line
do
USER=$(echo $line|cut -c 1-8)
CHOPPED=$(echo $line|sed 's/'$USER'/ /g'|sed 's/#/ /g')
FIRST=$(echo $USER|sed 's/#/ /g')
PTS=$(echo $CHOPPED|awk '{print $1}')
SYS=$(echo $CHOPPED|awk '{print $2}')
echo "$FIRST,$PTS,$SYS" >> result
done

The trick here is to replace blanks with a special character, cut first 8 chars, remove them from the line and use awk to seperate the fields and then replace the character back with a space. So, this script may give wrong results if that character is originally part of the failed login.

But you can use similar logic to achieve what you need.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Christian Gebhardt
Honored Contributor

Re: Need help with a script (fixing some formatting)

Hi

you can use awk and counting the fields not from the beginnig but from the end:

# cat inputfile
a b c d e
a c d e
# awk '{printf("%s %s\n",$(NF-1),$NF)}' inputfile
d e
d e
#


Chris
john korterman
Honored Contributor
Solution

Re: Need help with a script (fixing some formatting)

Hi Adam,
lastb -R seems to output lines with fields of fixed length. Why not just cut them up as in the attached script?

regards,
John K.
it would be nice if you always got a second chance
MAD_2
Super Advisor

Re: Need help with a script (fixing some formatting)

Sorry I did not reply to these postings until today, but I work Fri-Mon, and I could not really apply the answers until now that I got back. How is it that I did not apply John's logic from the beginning, very simple, but effective. I think I can really use that in this case.

Thanks everyone for your participation!
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with