[K12OSN] Add users from file

Les Mikesell les at futuresource.com
Wed Jun 9 15:44:24 UTC 2004


On Wed, 2004-06-09 at 07:49, Trond Ruud Maehlum wrote:
> Does someone know of a script which takes names from a file and automakes username?
> 
> I have a file with 300 names like so:
> 
> John,Smith
> 
> I want the script to go through the list and output another file like so:
> 
> John Smith,johsm (for example)... Alternatively an output which can be fed into the adduser batch in webmin.
> 
> Anybody seed something like this?

If you spend an hour or two learning stupid vi tricks (mostly
regexp substitutions), you'll be able to make those transformations
interactively in the editor at the drop of a hat for the rest of
your life.

For example, if you have a list like:
First1,Last1
First2,Last2
the vi command
:%s/\(^.*\),\(.\)\(.*\)/\1,\2\3,\L\1\2/
Will turn it into
First1,Last1,first1l
First2,Last2,first2l
and if you get it wrong, just hit 'u' to undo the change.

For the regexp challenged:
:  = start an ex (command line mode) command in vi
%  = shorthand for 1,$ in the range for the command
     (i.e. the whole buffer from the first to last line)
s  = substitute
/  = start 'match' portion
\( = start a grouping
^  = anchor to beginning of line
.  = any character
*  = any number of the previous
\) = end grouping
,  = literal match outside groupings
\(.\) = grouping that matches any one character
\(.*\) = grouping that picks up the rest
/  = start 'replace' portion
\1 = what the first grouping matched
,  = literal text
\2 = 2nd grouping (the one character)
\3 = 3rd grouping
\L = lowercase following
\1\2 = 1st and 2nd groups again.
/  = end replacement

One problem with complicated ex commands is that while you can
undo the operation on the buffer, you can't easily recall and
edit the command line itself.  I usually either open another
window and cut/paste to save, edit, and re-use it or type the
command as a normal line in the editor and use vi's delete-to-register,
execute-register, put-register commands to save and restore
it. For example, with the cursor on the line with the :command
typing "add will delete it to the "a" register.  Typing @a
will execute it.  If you don't like it, u will undo the edit
and "ap will put the line back in the editor.

---
  Les Mikesell
    les at futuresource.com






More information about the K12OSN mailing list