Operating System - HP-UX
1827942 Members
2512 Online
109973 Solutions
New Discussion

sed usage for report generation

 
SOLVED
Go to solution
Victor_5
Trusted Contributor

sed usage for report generation

I have two files, lastlog_model and report. See attached.

1. lastlog_model

It is a ASCII file that contains stanzas with the last login attributes for users.Each stanza is identified by a use name and contains attributes inthe Attribute=Value form. Each attribute is ended by a new line character, and each stanza is ended by an additional new-line character.

2. report

This is the file I want to generate for system accounting.

I am looking a script which can generate the second file according to the original one, file lastlog_model from AIX, but I know the shell should be same.

Any reply will be really appreciate.

5 REPLIES 5
David Lodge
Trusted Contributor

Re: sed usage for report generation

Try the following bit of awk, it probably won't be perfect for you, but it can be abused:

awk '
BEGIN {
FS="\n";
RS="";
OFS=" ";
ORS="\n";
}
{
for (i=1;i<=NF;i++) {
printf "%s ", substr($i,index($i,"=")+1)
}
printf "\n"
}'

HTH

dave
Sridhar Bhaskarla
Honored Contributor
Solution

Re: sed usage for report generation

Shawn,

Try this. This should work if your input file looks exactly the way you attached.

#!/usr/bin/ksh

if [ $# -ne 1 ]
then
echo "Usage: $0 inputfile"
exit 1
fi

FILE=$1
printf "%-20.30s %-20.20s \n" "user_name" "Time Last Login"
echo ""
for i in `grep -v "=" $FILE`
do
USER=`echo $i|sed 's/://'`
LINE=`sed -n '/'$i'/,/time_last_login/p' $FILE |grep last_login`
UTIME=`echo $LINE|awk '{print $3}'`
printf "%-20.30s %-20.20s \n" "$USER" "$UTIME"
done
-Sri
You may be disappointed if you fail, but you are doomed if you don't try
harry d brown jr
Honored Contributor

Re: sed usage for report generation

As you can see, you want to use awk or some shell script. sed is a stream text editor, for manipulating streams of characters.
Live Free or Die
Victor_5
Trusted Contributor

Re: sed usage for report generation

Hi Sridar:

Your script has a mini problem: there are some comments in the beginning of lastlog_model, the loop(grep -v "=" $FILE) does not filter those comments.

However, I still want to give you 10 points, your script is perfect, thank you so much.

Have a nice weekend.

Shawn
Sridhar Bhaskarla
Honored Contributor

Re: sed usage for report generation

Shawn,

It's simple. You just need to make these modifications.
...
....
fi

FILE=/tmp/file$$
sed '/^*/d' $1 > $FILE (If the comment is with *)
...
..
...
done
rm $FILE

Sorry for the overlook.

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