1

My file has 100 lines. I have to loop over all the lines and get the full name of the athletes.

Each line format looks like this but with different athletes:

vada359,Valerie Adams,Women,Athletics,AT,new-Zealand

I'm having a hard time coming up with a script that reads the line and creates new users with the useradd command - but I should only get the full name.

Can you help me on ideas on how I can do this?

Zanna
  • 70,465
  • Is the full name always in the second position (after the first comma)? – Elder Geek Sep 12 '16 at 19:59
  • 5
    This sounds a lot like homework. Review man cut for some ideas on how to manage the string handling required. – Elder Geek Sep 12 '16 at 20:04
  • yes that's the format of the file. The full name will always be in the second spot. – G_alvarez Sep 12 '16 at 20:04
  • yes it's a homework – G_alvarez Sep 12 '16 at 20:04
  • 1
    I hope to see you answer your own question! – Elder Geek Sep 12 '16 at 20:06
  • You tagged this with useradd command, why? And why should we help you with your homework? You were supposed to solve your homework right? – Anwar Sep 12 '16 at 20:13
  • thanks man, the professor expect us to know this already and this is an intro class, so more than half of the class is completely lost and will probably fail this assignment including me, i'm just trying to do some research to do whatever i can. Thanks for your help. – G_alvarez Sep 12 '16 at 20:15
  • i don't want you to do my homework, all i want are hints or suggestions on how could i get this done. I do my own work, i'm just new to scripting and it's kind of difficult for me – G_alvarez Sep 12 '16 at 20:18
  • 4
    Hints or suggestions are not easy to give if you don't have any knowledge of scripting. Instead, you should look for simple tutorials and try yourself. When you face a problem that fails your script, you can post your script here and you will more likely get help. – jiipeezz Sep 12 '16 at 20:26
  • @ElderGeek: Instead of using cut I'd rather set $IFS so I can just use read with a while loop to read the file. – Florian Diesch Sep 12 '16 at 20:29
  • Do you already have some code we can see? Edit your question and post your code, so we can help. – Thiago Rider Augusto Sep 12 '16 at 20:34
  • @FlorianDiesch Well, since cut includes a delimiter option and a fields option, I would think it would be a relatively simple exercise, but you clearly have more experience in this matter than I. – Elder Geek Sep 12 '16 at 20:36

1 Answers1

2

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

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497