Operating System - HP-UX
1819872 Members
2737 Online
109607 Solutions
New Discussion юеВ

Re: About awk,shell program!

 
SOLVED
Go to solution
SHENFENG_1
Occasional Advisor

About awk,shell program!

Hi,
I need a shell.It can use awk.I need this function,that using '/etc/passwd','/etc/group' and '/etc/last -1' to get a file.The file like:
UserUser GroupGroup $HOME
ID Name ID Name LastLoginDirectory
0 root 3 sys at Nov 15/

Who can help me?thanks!
8 REPLIES 8
Hein van den Heuvel
Honored Contributor

Re: About awk,shell program!

perl:

$file = "/etc/group";
open (FILE,"< $file") || die "Could not open $file";
while () {
($name,$x,$id) = split (/:/);
$group{$id} = $name;
}
close (FILE);

$file = "/etc/passwd";
open (FILE,"< $file") || die "Could not open $file";
while () {
($name,$pass,$id,$gid,$x,$pwd) = split (/:/);
$_ = `last -1 $name`;
@last = split;
$at = ($_) ? "at $last[3] $last[4]" : "";
print "$id $name $gid $group{$gid} $at $pwd\n";
}

SHENFENG_1
Occasional Advisor

Re: About awk,shell program!

Hein,
Thank you,can you use awk to program?I need it.
Hein van den Heuvel
Honored Contributor
Solution

Re: About awk,shell program!

ok.. because you ask so nicely... but you should really check out perl!

awk -f x.awk /etc/passwd


where x.awk contains:

BEGIN { FS = ":";
while(getline < "/etc/group") {
group[$3]=$1;
}
}
{ name = $1;
id = $3;
gid = $4;
pwd = $6;
FS = " ";
$0 = "hey, there was no time";
"last -1 " name | getline;
month = $4;
day = $5;
FS = ":";
print id,name,gid,group[gid],"at",month,day,pwd;
}
SHENFENG_1
Occasional Advisor

Re: About awk,shell program!

My machine OS is HP 11i,I run your code,then make some mistakes,help me!
$ awk -f x.awk /etc/passwd
awk: Cannot find or open file x.awk.
The source line number is 1.
The error context is
>>> <<<
$
$
$ where x.awk contains:
sh: where: not found.
$
$ BEGIN { FS = ":";
sh: BEGIN: not found.
$ while(getline < "/etc/group") {
sh: Syntax error: `{' is not expected.
$ group[$3]=$1;
sh: 3: Parameter not set.
$ }
sh: Syntax error: `}' is not expected.
$ }
sh: Syntax error: `}' is not expected.
$ { name = $1;
> id = $3;
> gid = $4;
> pwd = $6;
> FS = " ";
> $0 = "hey, there was no time";
> "last -1 " name | getline;
> month = $4;
> day = $5;
> FS = ":";
> print id,name,gid,group[gid],"at",month,day,pwd;
> }
sh: 1: Parameter not set.
Hein van den Heuvel
Honored Contributor

Re: About awk,shell program!

please re-read my awk reply carefully.

You were not supposed to cut & paste the whole thing!
I showed a sample command line with x.awk.

This x.awk was supposed to be a file (which you can call anythign else!) that you created with the text from the rest of the reply.

So x.awk should start with: "BEGIN { FS ..."
and the last lines would be:

print id,name,gid,group[gid],"at",month,day,pwd;
}

You can also put it all in a singel file by playing with <
Hein.



Ramalingam
Advisor

Re: About awk,shell program!

Hi ,
I think you have typed everything continuosly....

create a file x.awk
with the content given in the reply above..

Then in shell prompt give
awk -f x.awk /etc/passwd


Regards,
Ram.

We Learn the most When We have to Invent
SHENFENG_1
Occasional Advisor

Re: About awk,shell program!

Hein,
Sorry,because of busy,so I can't read your program carefully.
Then,I make a file named temp_test.awk,run it,successful.That is my wanted!
Thank you very muck!
Hein van den Heuvel
Honored Contributor

Re: About awk,shell program!

I'm glad you are happy now.

But I am a little dissapointed that you initially did not even bother in the slightest to understand the solution.
The when it blows up, that should give you a hint to check it out a little no?

Myself, I answer questions here NOT primarely to try to solve your problems, but to teach you how you could solve your problem, and others similar to it in the future.

I'm pretty sure most folks helping here feel similar.

"Give a man a fish, and you feed him
for one day. Teach a man to fish, and you feed him for a lifetime"

Kindest regards,
Hein.