0

The goal of the script is to get the last name and the first and last letter of the first name and provide is as an eventual username for the useradd command. An example of this is: John Doe, The desired username would be doejn.

I can get the CSV file read into the script by using this: (The CSV File has a Header of "First Name" and "Last Name")

employeeData = CSV.parse(File.read("employeedata.csv"), headers: true)

After that I have no clue how to use the data that is in the employeeData variable.

1 Answer 1

2

I am considering your employeedata.csv having a header with First Name and Last Name column

e.g of CSV file

First Name, Last Name
Rohit, Lingayat

filename = 'employeedata.csv' #*location of the file*

CSV.foreach(filename, headers: true) do |row|
   first_name_char = row['First Name'].strip.split('')
   useradd_name = "#{row['Last Name']}#{first_name_char.first}#{first_name_char.last}"
   puts useradd_name
end

output:

LingayatRt

You can use useradd_name to use in your command or you can create an array which will be the list of all combinations of the first name and last name and you can use accordingly.

hope this solution will help you out.

Sign up to request clarification or add additional context in comments.

3 Comments

If I keep the file variable just before headers: it errors out. And when I put the path to the CSV in its spot it also errors out.
what is the error? you need to specify the location of the file
file != filename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.