Need help with simple awk script

Need help with simple awk script

Post by Brian Locasc » Fri, 09 May 1997 04:00:00



Hello,

I am not much of a UNIX shell programmer (I am learning quickly
though), but I am trying to figure out a way to get awk (or other
utility) to parse thru a space delimited text file and send the
information the BSDI v3.0 adduser program.

The text file is actually a subset of a Linux password file that I
created using awk.  We are migrating from Linux to BSDI v3.0 and
don't want to add the users to the new machine by hand.  Therefore I
thought I would dump only the information from the Linux passwd file
that was necessary and feed it to the BSDI adduser program (with a few
switches).

The text file has around 500 lines in the format of

{username} {DES encrypted password} {First Name} {Last Name} {shell}

                (there is a space between each field)

The BSDI "addsuser" command has a whole bunch of switches, therefore I
will need to make the AWK script add users to the BSDI machine by
issuing these switches as well as the account information for each
user.

The AWK script will need to send the following info to adduser for
each line in the space delimited text file...:

adduser -g {groupname} -h {base home directory} -H {user's home
directory} -p {DES encrypted password} -s {shell}

I don't necessarily need someone to write the whole darn thing for me
(although that would be great), just knowing how to approach the task
and maybe giving me a general "outline" for the script would be a huge
help!!

Thanks!

 
 
 

Need help with simple awk script

Post by Caroline Farquhars » Fri, 09 May 1997 04:00:00




>{username} {DES encrypted password} {First Name} {Last Name} {shell}

>            (there is a space between each field)

>The BSDI "addsuser" command has a whole bunch of switches, therefore I
>will need to make the AWK script add users to the BSDI machine by
>issuing these switches as well as the account information for each
>user.

>The AWK script will need to send the following info to adduser for
>each line in the space delimited text file...:

>adduser -g {groupname} -h {base home directory} -H {user's home
>directory} -p {DES encrypted password} -s {shell}

There is a simpler way than awk in this instance. Try

BASEDIR=(Base directory) # Base directory for users
cat ( text file) | while read line # Read file a line at a time
do
        set -- line     # Split line into positional parameters.
                        # The fields will now be : -
                        # $1 username
                        # $2 passwd
                        # $3 first name
                        # $4 second name
                        # $5 shell
       adduser -g (groupname) -h $BASEDIR -H $BASDIR/$1 -p $2 -s $5 $1
done

This works here but wont work if: -

        There are more than 9 parameters
        The fileds are seperated with something other than a white
space or tab

 
 
 

Need help with simple awk script

Post by Tapani Tarvaine » Fri, 09 May 1997 04:00:00



> BASEDIR=(Base directory) # Base directory for users
> cat ( text file) | while read line # Read file a line at a time
> do
>    set -- line     # Split line into positional parameters.
>                    # The fields will now be : -
>                    # $1 username
>                    # $2 passwd
>                    # $3 first name
>                    # $4 second name
>                    # $5 shell
>        adduser -g (groupname) -h $BASEDIR -H $BASDIR/$1 -p $2 -s $5 $1
> done

> This works here but wont work if: -

>    There are more than 9 parameters
>    The fileds are seperated with something other than a white
> space or tab

Bourne sh can't handle more than 9 positional parameters but ksh can,
the syntax being ${10} &c.
Alternatively you can simply let the 'read' command separate them,
which IMHO is cleaner anyway.  Also, you don't need 'cat' as you can
redirect the file to the loop directly.  So:

while read username passwd firstname secondname shell ;do
    adduser -g $group -h $BASEDIR -H $BASDIR/$1 -p $passwd -s $shell $username
done < textfile

If there are more fields in the line than variables in the 'read',
the last variable gets everything to the end of the line (so you
might wish to add an extra variable, say, 'junk' at the end).

You can also handle any (single-character) separators by setting IFS.
E.g., if the fields were separated by commas or semicolons you'd do

while IFS=',;' read username passwd firstname secondname shell ;do
...

Be careful if you have empty fields, however.  Bourne sh will fold
any number of consecutive separators, whatever they are, so that
"1,,2,3" is equivalent to "1,2,3", whereas ksh counts consecutive
separators as distinct, so that "1,,2,3" has four fields, second
being empty, _except_ in the case of spaces and tabs, which are
still folded.

--
Tapani Tarvainen

 
 
 

1. Help with the simple awk script

Hello all,
I try to write an awk script to print the number from the 1st to the
2nd field.  For example, if my awk script is "test1", when I run it:
% test1 2 6
then the output should be:
2
3
4
5
6

I made this following script.  However, when I run, nothing display
on the screen:

awk 'BEGIN {
        a = $1
        b = $2
{  for (i=a; i<=b; i++) {
      {print i}
      {exit}
   }

What is wrong with my script?

I'm appreciated for any suggestion.

Nguyen

2. files that has not been modified for more than 4 hours

3. Simple AWK script help?

4. Remote X applications & window managers

5. Help with the simple awk script

6. SlimServer build

7. simple help with awk needed

8. Loadavg under kernel 2.0.0

9. Help: awk-scripts needed.

10. Help needed with a lookup script in AWK

11. Need help with awk/shell script do do Data massaging PLEASE!!!

12. Need help getting started on a script (awk, sed, ksh ?)

13. Please Help! need expiration script, shell or awk