Since this is pretty much a homework question, I won't give exact answer. However, here's some ideas that may lead you to solution.
Many tools allow splitting text into columns or "fields" at specific characters. In your case, you want to split text at commas, and extract field #2. Tools such as awk, allow specifying field separator with -F
option, and printing fields as $1, $2, $3 , etc. cut
as Elder Geek mentioned, is another tool for such job, and can use -d
for field delimiter and -fx
for fields, where x is an integer number.
Having extracted those fields, you will want to pass each one of them to useradd
or better adduser
, which is a Perl script designed specifically for easy addition of users to the system. If you're outputing each name from previous command via pipe to adduser
, you might want to use something like
command1 | xargs adduser
There's a bit of a problem however - you're going to be getting a string with first name and last name separated by space. Typical Unix username is done as single string, so I'd recommend you send the extracted name via tr -d ' '
command, which deletes spaces. That way you'll create a user ValerieAdams for example. Depending on your requirements this may or may not be acceptable, so review the requirements.
In addition, as Kaz Wolfe pointed out capitals in username are not allowed by default in adduser, however it's possible to use --force-badname
flag as per karthic87's answer
man cut
for some ideas on how to manage the string handling required. – Elder Geek Sep 12 '16 at 20:04cut
I'd rather set$IFS
so I can just useread
with awhile
loop to read the file. – Florian Diesch Sep 12 '16 at 20:29